views:

81

answers:

3

In Django is there a way to force admin users to choose to fill one of a few ForeignKeys and not more than one?

I have a model something like :

class URL(models.Model):
    ...
    links = models.URLField(_('Google Links'),verify_exists=True,unique=True)
    project = models.ForeignKey(Project,blank=True,null=True)
    category = models.ForeignKey(Category,blank=True,null=True)
    person = models.ForeignKey(ExternalPerson,blank=True,null=True)
    ...

I want the admin user to choose one of the Foreignkeys project,category or person. Or should I organize the model differently?

+1  A: 

Just some ideas...

I don't know what you want to do later on with those objects, but have you consider to create a common interface to all of them? It could fix your problem...

If the interface doesn't suit you,maybe you could create a Form to check that just one of the ForeignKeys have been selected.

Jesus Benito
A: 

Im interested in knowing how you solved this, Im looking for the same thing.

I didn't solve it. I might basically an ugly hack : I made an extra ChoiceField to select which one of the three Foreignkeys are used. The others are ignored.
Jasper
+1  A: 

What I did is to have a single foreign key that points to a base model, from which all the other models (Google Link, Projects, etc) inherit. That seems to maintain the relationship while restricting the foreign key to a single choice.

That sounds like a viable solution. I will try this. thanks.
Jasper