Представление django, которое объединяет данные модели со всеми полями ее родителяPython

Программы на Python
Anonymous
Представление django, которое объединяет данные модели со всеми полями ее родителя

Сообщение Anonymous »


I'm creating a django app where I have a model with a parent class:

class BaseModel(models.Model): modified_date = models.DateTimeField(auto_now=True) class Meta: abstract = True class Site(BaseModel): name = models.CharField( max_length=25, verbose_name="Site Name (Internal) (25 char)", null=True ) and I've created a view to return the object as JSON. However, when I query and get the objects with Site.objects.all(), it doesn't contain the data from its parent class -- just the data from its own model.

Now, I can go ahead and grab the primary keys and manually query the two datasets and consolidate them, but I bet there has to be a django / pythonic way to do this in a single query. prefetch_related() and select_related() dont seem to do what I want, or at least I havent figured out how to make them work for me.

Does anyone have suggestions?


Источник: https://stackoverflow.com/questions/780 ... its-parent

Вернуться в «Python»