How to instantiate related models in Django

I am working on a CMSy application for which I have implemented a set of models that allow you to create custom template instances, consisting of several fields and tied to a specific client. The ultimate goal is that one or more templates with a set of custom fields can be defined through the admin interface and connected to the client so that the client can then create content objects in the format prescribed by the template.

It seems that I have connected so that I can create any number of Template objects, but I'm struggling with how to create instances - the actual content objects - in these templates. For example, I can define the “Primary Page” template for the “Acme” client, which has the “Title” and “Body” fields, but I did not understand how to create Basic Page instances where these fields can be filled.

Here are my (somewhat stylized) models ...

class Customer(models.Model):
    ...

class Field(models.Model):
    label = models.CharField(max_length=255)
    component = models.ForeignKey(ContentType, 
            limit_choices_to={'id__in': component_choices}
            )
    fields = models.Manager()

class Template(models.Model):
    label = models.CharField(max_length=255)
    clients = models.ManyToManyField(Customer, blank=True)
    fields = models.ManyToManyField(Field, blank=True)

class ContentObject(models.Model):
    label = models.CharField(max_length=255)
    template = models.ForeignKey(Template)
    author = models.ForeignKey(User)
    customer = models.ForeignKey(Customer)
    mod_date = models.DateTimeField('Modified Date', editable=False)
    def __unicode__(self):
        return '%s (%s)' % (self.label, self.template)
    def save(self):
        self.mod_date = datetime.datetime.now()
        super(ContentObject, self).save()

Thanks in advance for any advice!

+3
source share
4 answers

If you want to adhere to this model architecture, you need to add another field to the ContentObject class, which will serve to store the actual content. It could be something like:

ContentObject(models.Model):
    ...
    fields_content = models.ManyToManyField(Field, through=FieldContent)
    ...

:

class FieldContent(models.Model):
    field = ForeignKey(Field)
    content_object = ForeignKey(ContentObject)
    content = CharField

, , , , , pre_save, ContentObject, , FieldContent.

, :

FieldContent.objects.create(field=your_field, content_object=your_content_object, content=content_given_by_the_customer)
+1

:

my_customer.templates.add(my_template)

my_customer my_template Customer Template .

0

, . , , .

My Template , . ContentObject .

!

0

pawartur, ( ) . , , - :

class FieldContent(models.Model):
    field = models.ForeignKey(Field)
    content_object = models.ForeignKeyKey(ContentObject)
    def get_component(self):
        """Return the specific component instance for this field."""
        return self.field.get_object_for_this_type(pk=self.pk)

, :

class SomeComponent(FieldContent):
    # specific fields for the component here

, :      somecontentobject.get(ID = ).fieldcontent_set.get().get_component(). specific_component_field

, , :

    def create_content(self, content_object):
        return self.component.get_class().objects.create(
          content_object=content_object, field=self)

, , - , , , , , Django (, ..), , , , , , Django-cms (django-cms.org), , .

: - , - , .

0

Source: https://habr.com/ru/post/1737780/


All Articles