I have a question about a ForeignKey reference problem with django.
This is a part of my code :
App ticketsTrader
class TicketsTrader(models.Model):
seller = models.ForeignKey(User, related_name='ticketsTrader_seller')
buyer = models.ForeignKey(User, related_name='ticketsTrader_buyer')
inscription = models.ForeignKey(Inscription)
transactionCode = models.CharField(max_length=30,blank=False,null=False)
...
App inscription
class Event(models.Model):
title = models.CharField(max_length=75)
description = models.TextField()
...
class Inscription(models.Model):
event = models.ForeignKey(Event)
packs = models.ManyToManyField(PackChoise)
user = models.ForeignKey(User)
...
def __unicode__(self):
return self.event.__unicode__() + u': ' + self.user.__unicode__()
def inscriptionKey(self):
return str(self.pk) + '_' + str(self.valkey)
But when I try to acces to the “Add Ticket Trader” interface in my Grapelli admin, I get an error message :
User matching query does not exist.
Python27\lib\site-packages\django\db\models\query.py in get, line 366
In template \grappelli\templates\admin\includes\fieldset.html, error
at line 19
What I want to get is : in the “inscription” column of my ticketTrader table get the value of the unique id (pk) of my “Inscription” table.
Or the value of the “inscriptionKey” but I don’t think it’s possible.
Django Version:1.4 / Python Version: 2.7.3 / South last version
Thanks for your help 🙂
I can only guess:
TicketsTrader has a foreign key to Inscription, which has a foreign key to User and uses that key in its
__unicode__()method.Now, I don’t know Grapelli but the default admin app would render a dropdown for Inscription-s on the Add TicketsTrader page. If some of the
Inscription-s pointed to non-existingUser-s, then their__unicode__()method would fail with the error message you specified.The question is how could some
User-s be missing andInscription-s pointing to them not. Well, if you use e.g. MySQL+MyISAM, foreign keys are not enforced there so all sorts of weird things can happen.