Answer the question
In order to leave comments, you need to log in
How to implement feedback from descendants of an abstract model using the contenttypes framework?
Peace to all and Happy New Year!
And so, comrades, the question is this:
There is an abstract model "PostAbstract" and in each section of the site there is one inherited from "PostAbstract", in the future I will simply call them posts. In the "root" up there are Photo and Video models, which are already associated with the posts. For this connection, I use the contenttypes framework, though I can’t figure out how to implement feedback from posts to Photo and Video, because it’s impossible to describe the GenericRelation field in an abstract model.
root/models.py
class Post(models.Model):
"""Base class of Post on site"""
sources = []
def __init__(self, *args, **kwargs):
# "Owner"(owner) is required field! You can define that in instances of this class
if "owner" not in vars(self.__class__):
raise Exception("Field \"owner\" is undefined!")
else:
super().__init__(*args, **kwargs)
# meta information
create_date = models.DateField(auto_now_add=True)
modify_date = models.DateField(auto_now=True)
description = models.TextField()
size = models.PositiveSmallIntegerField(
editable=False, default=0, verbose_name="Колличество вложенностей")
def create_photo(self, description, src, file):
photo = Photo(description=False,
src=src,
file=file,
content_object=self
)
photo.save()
return photo
def create_video(self):
pass
def get_sources(self):
photos = Photo.objects.get(content_object=self)
print(photos)
pass
def save(self, force_insert=False, force_update=False, using=None,
update_fields=None):
return super(Post, self).save()
class Meta:
abstract = True
class Photo(models.Model):
# GenericForeignKey Felds
content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE)
object_id = models.BigIntegerField()
content_object = GenericForeignKey('content_type', 'object_id')
# meta information
create_date = models.DateField(auto_now_add=True)
modify_date = models.DateField(auto_now=True)
# Main Fields
description = models.TextField(null=True, blank=True)
src = models.CharField(max_length=500, blank=True)
file = models.FileField(null=True, blank=True)
class Meta:
unique_together = (("object_id", "src"),)
class Video(models.Model):
# GenericForeignKey Felds
content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE)
object_id = models.BigIntegerField()
content_object = GenericForeignKey()
# meta information
create_date = models.DateField(auto_now_add=True)
modify_date = models.DateField(auto_now=True)
# Main Fields
description = models.TextField()
src = models.CharField(max_length=500, blank=True)
file = models.FileField(null=True, blank=True)
class Meta:
unique_together = (("object_id", "src"),)
# модель наследуемая от root.models.Post
class MoviePost(Post):
owner = models.ForeignKey(Movie, on_delete=models.CASCADE)
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question