Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ForeignKey to a Model field?

I want a foreign key relation in my model with the username field in the User table(that stores the user created with django.contrib.auth.forms.UserCreationForm).

This how my model looks:

class Blog(models.Model):
    username = models.CharField(max_length=200) // this should be a foreign key
    blog_title = models.CharField(max_length=200)
    blog_content = models.TextField()

The username field should be the foreign key.The Foreign Key should be with this field

like image 959
Anish Silwal Avatar asked Jan 19 '16 11:01

Anish Silwal


People also ask

WHAT IS models ForeignKey?

ForeignKey is a Django ORM field-to-column mapping for creating and working with relationships between tables in relational databases. ForeignKey is defined within the django. db.

Can a model have a ForeignKey to itself?

Self-referencing foreign keys are used to model nested relationships or recursive relationships. They work similar to how One to Many relationships. But as the name suggests, the model references itself.

What is the difference between ForeignKey and OneToOneField?

A one-to-one relationship. Conceptually, this is similar to a ForeignKey with unique=True , but the "reverse" side of the relation will directly return a single object. In contrast to the OneToOneField "reverse" relation, a ForeignKey "reverse" relation returns a QuerySet .

Can a model have two foreign keys?

Models can have multiple foreign keys. The best design will depend on how you plan on querying the database. My first pass would be something like: Team model has team name etc.


2 Answers

Unless I'm missing something, you can have a ForeignKey to a specific field:

class Blog(models.Model):
    username = models.ForeignKey(User, to_field='username')

https://docs.djangoproject.com/en/1.11/ref/models/fields/#django.db.models.ForeignKey.to_field

like image 132
jthewriter Avatar answered Oct 17 '22 17:10

jthewriter


You can't have an ForeignKey to a field, but you can to a row.

You want username which is available through the User model

So:

blog.user.username

If you insist on having blog.username you can define a property like this:

from django.db import models
from django.contrib.auth.models import User

class Blog(models.Model):
    user = models.ForeignKey(User)

Then to access the field you want use:

blog.user.username

If you insist on having blog.username you can define a property like this:

from django.db import models
from django.contrib.auth.models import User

class Blog(models.Model):
    user = models.ForeignKey(User)

    @property
    def username(self):
        return self.user.username

With that property, you can access username through blog.username.

Note on how to import User

user = ForeignKey('auth.User')

or

from django.contrib.auth.models import User
user = ForeignKey(User)

or the more recommended

from django.conf import settings
user = ForeignKey(settings.AUTH_USER_MODEL)
like image 43
bakkal Avatar answered Oct 17 '22 17:10

bakkal