29👍
Django uses some python magic to define relationships between models, some of which involves using the name of the models in the relationships (that’s where the ‘test’ in ‘test__set’ is coming from.) What’s happening, I would guess, is that it’s trying to put “test__set” in the Example model twice, once for each foreign key you’ve got defined.
The error message suggests something to try: define a related_name
argument (overriding one of those ‘test_set’s) that it can use instead of auto-generating two clashing names.
More info here: page has been removed
Current page relating to model relationships:
https://docs.djangoproject.com/en/2.0/ref/models/fields/#module-django.db.models.fields.related
130👍
Try using related_name
:
class Test(models.model):
example1 = models.ForeignKey('Example', related_name='example1')
example2 = models.ForeignKey('Example', related_name='example2')
- [Django]-How to add the current query string to an URL in a Django template?
- [Django]-Is it bad to have my virtualenv directory inside my git repository?
- [Django]-Django filter JSONField list of dicts
8👍
Just do what the error message tells you to do, and if you’re unsure what that means, consult the documentation for related_name
.
- [Django]-'staticfiles' is not a valid tag library: Template library staticfiles not found
- [Django]-Django optional URL parameters
- [Django]-Django – "Incorrect type. Expected pk value, received str" error
4👍
In django 2.0 Try this:
user = models.ForeignKey(User, on_delete=models.PROTECT, null=True, related_name='user')
paper = models.ForeignKey(paperRecord, on_delete=models.PROTECT, null=True, related_name='paper')
- [Django]-How do you detect a new instance of the model in Django's model.save()
- [Django]-Django Template Language: Using a for loop with else
- [Django]-How to Unit test with different settings in Django?