Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use dynamic foreignkey in Django?

I want to connect a single ForeignKey to two different models.

For example:

I have two models named Casts and Articles, and a third model, Faves, for favoriting either of the other models. How can I make the ForeignKey dynamic?

class Articles(models.Model):     title = models.CharField(max_length=100)     body = models.TextField()  class Casts(models.Model):     title = models.CharField(max_length=100)     body = models.TextField()  class Faves(models.Model):     post = models.ForeignKey(**---CASTS-OR-ARTICLES---**)     user = models.ForeignKey(User,unique=True) 

Is this possible?

like image 929
Anakin Avatar asked May 19 '09 09:05

Anakin


People also ask

What is ForeignKey Django?

ForeignKey is a Django ORM field-to-column mapping for creating and working with relationships between tables in relational databases.

What is the difference between ForeignKey and OneToOneField?

The only difference between these two is that ForeignKey field consists of on_delete option along with a model's class because it's used for many-to-one relationships while on the other hand, the OneToOneField, only carries out a one-to-one relationship and requires only the model's class.

Is ForeignKey in Django one to many?

ForeignKey() definition creates the one to many relationship, where the first argument Menu indicates the relationship model.


2 Answers

Here is how I do it:

from django.contrib.contenttypes.models import ContentType from django.contrib.contenttypes import fields   class Photo(models.Model):     picture = models.ImageField(null=True, upload_to='./images/')     caption = models.CharField(_("Optional caption"),max_length=100,null=True, blank=True)      content_type = models.ForeignKey(ContentType)     object_id = models.PositiveIntegerField()     content_object = fields.GenericForeignKey('content_type', 'object_id')  class Article(models.Model):     ....     images     = fields.GenericRelation(Photo) 

You would add something like

    content_type = models.ForeignKey(ContentType)     object_id = models.PositiveIntegerField()     content_object = fields.GenericForeignKey('content_type', 'object_id') 

to Faves and

    fields.GenericRelation(Faves) 

to Article and Cast

contenttypes docs

like image 117
vikingosegundo Avatar answered Oct 03 '22 04:10

vikingosegundo


Here's an approach. (Note that the models are singular, Django automatically pluralizes for you.)

class Article(models.Model):     title = models.CharField(max_length=100)     body = models.TextField()  class Cast(models.Model):     title = models.CharField(max_length=100)     body = models.TextField()  FAVE_CHOICES = (      ('A','Article'),     ('C','Cast'), ) class Fave(models.Model):     type_of_fave = models.CharField( max_length=1, choices=FAVE_CHOICES )     cast = models.ForeignKey(Casts,null=True)     article= models.ForeigKey(Articles,null=True)     user = models.ForeignKey(User,unique=True) 

This rarely presents profound problems. It may require some clever class methods, depending on your use cases.

like image 41
S.Lott Avatar answered Oct 03 '22 05:10

S.Lott