Skip to content

Instantly share code, notes, and snippets.

@egitimplus
Created November 5, 2020 21:30
Show Gist options
  • Save egitimplus/9422ac21077b48b6943bec5185f0116f to your computer and use it in GitHub Desktop.
Save egitimplus/9422ac21077b48b6943bec5185f0116f to your computer and use it in GitHub Desktop.
from django.db import models
from django.contrib.contenttypes.fields import GenericForeignKey
from django.contrib.contenttypes.fields import GenericRelation
from django.contrib.contenttypes.models import ContentType
class Car(models.Model):
name = models.CharField(max_length=255)
content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE)
object_id = models.PositiveIntegerField()
content_object = GenericForeignKey()
class Sony(models.Model):
name = models.CharField(max_length=255)
cars = GenericRelation(Car)
class Pioneer(models.Model):
name = models.CharField(max_length=255)
cars = GenericRelation(Car)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment