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
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