123π
I havenβt done this yet, but I used inspectdb to generate the models.py file from an existing DB that does exactly that β this is what inspectdb threw back, so it should work:
creator = models.ForeignKey(Users, null=True, related_name='creator')
assignee = models.ForeignKey(Users, null=True, related_name='assignee')
Hope that works for you β if it doesnβt I am going to have a problem too.
9π
I think what youβre looking for is the related_name property on ForeignKeyFields. This will allow you to reference the same table, but give django special names for the relationship.
More Info:
- [Django]-Loading initial data with Django 1.7+ and data migrations
- [Django]-Why doesn't django's model.save() call full_clean()?
- [Django]-Feedback on using Google App Engine?
8π
Using related_name was my solution:
class Sample(models.model):
...
class Mymodel(models.model):
example1 = models.ForeignKey(Sample, related_name='sample1')
example2 = models.ForeignKey(Sample, related_name='sample2')
- [Django]-Django exception middleware: TypeError: object() takes no parameters
- [Django]-Complete django DB reset
- [Django]-Django-celery: No result backend configured
7π
From the error message, it sounds like youβre trying to put two foreign keys to the same object on an intermediary table used via the through
argument to ManyToManyField
, the documentation for which states:
When you set up the intermediary
model, you explicitly specify foreign
keys to the models that are involved
in the ManyToMany relation. This
explicit declaration defines how the
two models are related.There are a few restrictions on the
intermediate model:
- Your intermediate model must contain one β and only one β foreign key to
the target model (this would be Person
in our example). If you have more than
one foreign key, a validation error
will be raised.- Your intermediate model must contain one β and only one β foreign key to
the source model (this would be Group
in our example). If you have more than
one foreign key, a validation error
will be raised.
- [Django]-How to create a fixture file
- [Django]-TextField missing in django.forms
- [Django]-Django ModelForm to have a hidden input
-2π
The fact that two columns are part of one table implies that the two fields are related, therefor to reference them individually is not ideal. The ForeignKey of your model should be the primary key of the table you are referencing:
event = models.ForeignKey('event')
You would then reference the columns as such:
foo.event.actor
foo.event.receiver
If you wish you could also change the way your class/model references the foreign attributes with properties. In your class you would do the following:
@property
def actor(self):
return self.event.actor
@property
def receiver(self):
return self.event.receiver
This would allow you to then call foo.actor and foo.receiver but I believe the longer, foo.event.actor would be more pythonic
- [Django]-Forbidden (403) CSRF verification failed. Request aborted. Reason given for failure: Origin checking failed does not match any trusted origins
- [Django]-Django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module: No module named MySQLdb
- [Django]-Django delete FileField