Model Best Implementation

Hi,

Can you please help me design these models below for my project?

.model

One Container object have predefined amount (can be updated i.e. add more or deduct amount) of Sections when instantiated and unlimited amount of Boxes

Thanks.
John

What is your current level of knowledge or experience with either Django or databases? (What other Django work have you done?)

I’d say somewhere in between beginner to intermediate. I’ve created a couple of websites using Django, one, which sends dynamic emails to parents regarding their child’s attendance and another just a simple website for a real estate.

Do I need a Model Manager to be able to update the number of sections? How can the sections be indexed i.e. Sections 1 to n…?

This is what I currently have:

class Container (models.Model):
    title = models.CharField(max_length=200)
    description = models.TextField()
    number_of_sections = models.IntegerField(default=8)

class Section(models.Model):
    status = models.IntegerField(choices=STATUS_SECTION, default=0)
    updated_on = models.DateTimeField(auto_now=True)
    container = models.ForeignKey(Post, on_delete= models.CASCADE,related_name='section_containers')

class Box(models.Model):
    author = models.ForeignKey(User, on_delete= models.CASCADE,related_name='literature_blocks')
    section = models.ForeignKey(Section, 

on_delete=models.CASCADE,related_name=‘box_sections’)
text = models.TextField()
updated_on = models.DateTimeField(auto_now=True)
created_on = models.DateTimeField(auto_now_add=True)

Ok, so what’s your initial idea for the model design? What comes to mind when you see a set of relationships like this?

1 Like

Sorry, I have edited the previous response.

No, you don’t need a Manager for that. Whatever facility you create for adding a Section to a Container would check to see if that’s an allowable operation based upon the upper limit.

If you want the Sections to be in some specific order, then the easiest way to handle that is through a “order” field in the Section model.

Side note - I think you want the reference to be to Container, not Post

Thanks Ken. I forgot to rename the ForeignKey to match the drawing.

Any advice (codes perhaps) on how to implement such facility? What about removing a section?

Thanks for your help.

Given a Container, the number of Section is:
section_count = Section.objects.filter(container=Container).count()

So when you’re building your UI, if number_of_sections == section_count, then you disable whatever widget would allow a user to add a new section. You also want to perform the same comparison when the form to add a new section is submitted.

I’m not sure I understand what you’re asking with “What about removing a section?”

Ken

Thanks! So, do it from the UI.

The author would have the option to dynamically add or remove sections from the form. For example, upon creating the container, the author decided that only 8 sections were needed, then realised afterwards that it only requires 7 sections, therefore had to remove or decrease the number of sections . This is what I meant by removing a section. Sorry, if I am not explaining that well. Let me know if it makes sense.

Regards,
Alvin

Ok, I understand.

But that raises a more fundamental question in my mind. If the author has the ability to add and remove sections at will, what’s the purpose behind identifying a maximum number in the container? I’m not seeing where managing that number adds value to the process.

The purpose of adding a maximum number is to inform and constrain potential collaborators from adding their ideas only on the predefined sections i.e. sections 1 to 8. It’s kind of a unique feature of the app. Like Twitter for example, it has character constraints that may trigger creativeness from users, as they have to constantly summarise (be creative) with what they’re trying to convey.

So for my understanding, it sounds like there are two roles - what I’ll call “author” and “collaborator” based on words you’ve used earlier.

Only the author can add / remove Section, but a collaborator can only add or remove(?) a box.

I still don’t see the need for a “maximum number of sections” count. If a collaborator can’t add a Section, then they are, by definition, limited to those predefined by the author.

Regarding the idea of removing a Section - what happens to a Box that had previously been entered in that Section?

Sorry for the delay. I have work today.

The boxes inside the removed section/s are hidden or logged or tagged. The “number of sections” is pretty much a rule. Like when writing a Haiku, for example, this is the rule that must be followed to identify such poem as Haiku. Same with this app, the author would declare the rule (number of sections) that is enforced.

There are two different ways to enforce that - with tradeoffs between them.

  • Author creates Sections. Contributors can only add Boxes to the created Sections.

  • Author defines number of Sections. Contributors can add Sections up to the limit, and then add Boxes to Sections.

If the Author is the only Role capable of adding Sections, then having a separate variable to contain the number of Sections is redundant.

Thanks, Ken.

I think I get your point, but how do I instantiate a container with such amount of sections?

Can you please show me how you would create these models? I just can’t seem to wrap my head around it - I’m having difficulty visualising it. Thanks again for your time.

You create the models the same way you create any model.

Assuming you have a view where the Author supplies the information for the Container in a form, you can add a field for the number of sections to be created.
In the view that handles the POST, save the Container, then create a number of Sections equal to the number entered in the form.

I can be more specific if you include the view and form that you’re using for creating the Container.

1 Like

I get what you’re saying now.

Thanks so much for your time Ken, I really appreciate your help.