How to create instances of related models in Django

Posted by sevennineteen on Stack Overflow See other posts from Stack Overflow or by sevennineteen
Published on 2010-03-20T04:00:10Z Indexed on 2010/03/20 4:01 UTC
Read the original article Hit count: 286

Filed under:
|
|

I'm working on a CMSy app for which I've implemented a set of models which allow for creation of custom Template instances, made up of a number of Fields and tied to a specific Customer. The end-goal is that one or more templates with a set of custom fields can be defined through the Admin interface and associated to a customer, so that customer can then create content objects in the format prescribed by the template.

I seem to have gotten this hooked up such that I can create any number of Template objects, but I'm struggling with how to create instances - actual content objects - in those templates. For example, I can define a template "Basic Page" for customer "Acme" which has the fields "Title" and "Body", but I haven't figured out how to create Basic Page instances where these fields can be filled in.

Here are my (somewhat elided) models...

class Customer(models.Model):
    ...

class Field(models.Model):
    ...

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!

© Stack Overflow or respective owner

Related posts about django

Related posts about models