Skip to content

Instantly share code, notes, and snippets.

@egitimplus
Created November 4, 2020 20:39
Show Gist options
  • Save egitimplus/4ab39e2bdfa7d028ed8c81c386d780dd to your computer and use it in GitHub Desktop.
Save egitimplus/4ab39e2bdfa7d028ed8c81c386d780dd 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 CarModel(models.Model):
name = models.CharField(max_length=255)
content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE)
object_id = models.PositiveIntegerField()
content_object = GenericForeignKey()
def __str__(self):
return self.name
class Sony(models.Model):
name = models.CharField(max_length=255)
carmodels = GenericRelation(CarModel)
def __str__(self):
return self.name
class Pioneer(models.Model):
name = models.CharField(max_length=255)
carmodels = GenericRelation(CarModel)
def __str__(self):
return self.name
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment