Best Model Architecture?

54 views
Skip to first unread message

Avery Laird

unread,
Jan 26, 2015, 10:33:27 PM1/26/15
to mezzani...@googlegroups.com
Hi,

I have a question about best practices / tips for making my models a bit less verbose and confusing. For example, I often find when making models for a certain theme that I repeat myself. Let's say we have a page with multiple elements: a slider, two bodies of text (each with their own header), an accordion with it's own header, some quotes from clients, and maybe a some team members. Usually, most of this content layout is consistent through the site, but with different data (except for a few cases, ie it's unlikely the team model would have differing data for different pages). Is there a way to make a class for each element which the page itself can inherit, much like the RichTextField (even though I know RichTextField is not the perfect example)? And if so, what should that model inherit from? Something like this...

class Slider(Orderable):
     
# eg
image = FileField(verbose_name=_("Image"),
                      upload_to=upload_to("theme.Slide.image", "slider"),
                      format="Image", max_length=255, null=True, blank=True)
Class Meta:
   
# whatever


...would have to define a homepage, and then be initialized as inline in admin.py. Is there a way to instead have the page inherit the model, or at least not access everything through inline (even though it is a model separate from the page)? Or at least, a more streamlined process that doesn't involved me repeating myself?

Thanks in advance for any insight, I hope I explained myself adequately! 

Cheers,

Avery

Josh Cartmell

unread,
Jan 27, 2015, 10:58:53 AM1/27/15
to mezzani...@googlegroups.com
Hi Avery, you should be able to do this:

class SlideBase(models.Model):
     
# eg
image = FileField(verbose_name=_("Image"),
                      upload_to=upload_to("theme.Slide.image", "slider"),
                      format="Image", max_length=255, null=True, blank=True)
Class Meta:

  
abstract = True


Then:

class Slider(Orderable, BaseSlider):
    pass

The key here is making BaseSlider abstract have a look at the docs if you want to dive in more, https://docs.djangoproject.com/en/1.7/topics/db/models/#abstract-base-classes

Avery Laird

unread,
Jan 27, 2015, 5:50:21 PM1/27/15
to mezzani...@googlegroups.com
Hi Josh,

Thanks for the reply. I actually already use abstract models in many of my projects -- very useful feature! Maybe I should have explained my thinking a bit better. I was describing something more along the lines of the RichTextPage behaviour. I know the mechanics behind the scences of RichTextPage is actually more complicated than this, but the behavior that we see involves a page (eg, class Page(Page, RichTextPage) ) which inherits RichTextPage. Is it possible to do something like that? For example, I know I also want my page to have a slider, so I do: class Page(Page, Slider). Let's say I also want an accordion: class Page(Page, Slider, Accordion). The level lower than that (BaseSlider, BaseAccordion)  would involve the abstract models you talked about above. Being pretty new to django, I have some large gaps in my knowledge which may have prevented me from seeing some glaring error in this thinking --  is this even possible / practical?

Thanks again, I appreciate your help with this!

--
You received this message because you are subscribed to a topic in the Google Groups "Mezzanine Users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/mezzanine-users/HVAjvY2ZfCY/unsubscribe.
To unsubscribe from this group and all its topics, send an email to mezzanine-use...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Josh Cartmell

unread,
Jan 28, 2015, 11:14:25 AM1/28/15
to mezzani...@googlegroups.com
Gotcha, I think I'm understanding better what you want to do.

I'm not sure this is possible because presumably the Slide class will need a foreignkey to the parent page class that it shows up on, is that correct?  I.e. wouldn't the models really look something like this:

class SliderPage(Page):
    pass


class Slide(Orderable):
    sliderpage = models.ForeignKey(SliderPage, related_name="slides")
    image = FileField(...)

Then wherever you had an instance of a SliderPage you could do:

sliderpage.slides.all()

I don't know of any way to inherit a ForeignKey relationship (that of course doesn't mean it's impossible).

Something else you may be able to do is use a generic foreignkey in some way, https://docs.djangoproject.com/en/1.7/ref/contrib/contenttypes/#generic-relations, that would allow you to have a slide be related to any other model, but it would also complicate things a lot more.


--
You received this message because you are subscribed to the Google Groups "Mezzanine Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mezzanine-use...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages