0

I am trying to setup different models in django.

Some of my models includes fields for a text. A text is defined by: - CharField (tex) - CharField (font-size) - CharField (font-weight) - CharField (color)

So some of my models need one to n of these texts.

Is it possible to create a collection of Fields, for example "Test-Collection" that includes all 4 fields. So that i didn't have to write all 4 field manually for each text i need in a model?

Some thing like that:

class Box(CMSPlugin):
    text1 = models.CharField(max_length=100)
    text1_font_weight = models.CharField(max_length=100)
    text1_font_size = models.CharField(max_length=100)
    text1_color = models.CharField(max_length=100)

    text2 = models.CharField(max_length=100)
    text2_font_weight = models.CharField(max_length=100)
    text2_font_size = models.CharField(max_length=100)
    text2_color = models.CharField(max_length=100)

    text3 = models.CharField(max_length=100)
    text3_font_weight = models.CharField(max_length=100)
    text3_font_size = models.CharField(max_length=100)
    text3_color = models.CharField(max_length=100)

Into that:

class Box(CMSPlugin):
    text1 = TextColelction...
    text2 = TextColelction...
    text3 = TextColelction... 
Flo
  • 1,179
  • 3
  • 15
  • 43

2 Answers2

0

Maybe its better to define a separate model for text and have a ForeignKey relation to the Box. For example:

class Text(models.Model):
    text = models.CharField(max_length=100)
    text_font_weight = models.CharField(max_length=100)
    text_font_size = models.CharField(max_length=100)
    text_color = models.CharField(max_length=100)

class Box(..):
   text1 = models.ForeignKey(Text)
   text2 = models.ForeignKey(Text)
   text3 = models.ForeignKey(Text)
ruddra
  • 50,746
  • 7
  • 78
  • 101
  • Thanks for your answer! But than if i want to include that taxt in multiple other classes? – Flo Nov 19 '18 at 18:11
  • @Flo I have added an update section, please have a look. – ruddra Nov 19 '18 at 18:16
  • That is going the way i like... but its quite inconvenient that i can't create the texts inside the the "new box screen" – Flo Nov 19 '18 at 18:26
  • sorry, i did not get that, what do you mean by *new box screen*? is it another model? – ruddra Nov 19 '18 at 18:28
  • In the Admin panel if i create a new box model then i would like that the text "form" is nested inside the box "form" – Flo Nov 19 '18 at 18:29
  • @Flo I have added Inline related codes, using that you can add texts in new box page – ruddra Nov 19 '18 at 18:36
  • there is no way to store the relationship on parent or? – Flo Nov 19 '18 at 20:38
  • sorry, i did not get that. What do you mean by `relationship on parent or`? – ruddra Nov 19 '18 at 20:40
  • Normaly the text stores the id from the box... i would it like the other way that the id of text is stored in box – Flo Nov 19 '18 at 20:44
  • why would you need to do that? because you can access the texts(`Text` objects) from box(`Box` object) and get the id of the texts from box. – ruddra Nov 19 '18 at 20:46
  • So that i can reuse the text object in other models too – Flo Nov 19 '18 at 20:47
  • What do you mean exactly by re-using in other models? – ruddra Nov 19 '18 at 20:47
  • Okay i think i have to describe that more: I have different models that need to have one ore more "Texts" embeded. So that i can use the same text model on Box-element and on Container-Element and some others... – Flo Nov 19 '18 at 20:49
  • Ah. In that case, you can make the Texts independent, and have a many to many relationship. I will update the code shortly – ruddra Nov 19 '18 at 20:51
  • But why that is a ManyToMany? I Thing that is a OneToOne? – Flo Nov 19 '18 at 20:56
  • you can't have one to one, because OneToOne means, one Box can only be connected to One Text only, but you might need atleast four Texts – ruddra Nov 19 '18 at 20:57
  • Yeah but i would like to store each "relation" in a single attribute! I would like to have a text1, text2 and text3 attribute on Box, not a list of Texts And on Container i would like to have text1 and text2 but no third one – Flo Nov 19 '18 at 21:00
  • Now i will get the following error: 'Text' has no ForeignKey to 'Box'. If i use "InlineAdmin" – Flo Nov 19 '18 at 21:41
  • You can't use inline admin. It works only for reverse relationship – ruddra Nov 19 '18 at 21:45
  • Thats so annoying... but a very very very big THANK YOU for your support! – Flo Nov 19 '18 at 21:46
  • I don't think you need bthat either. You can directly add a text or create one from box admin. Please accept this answer if it helps you – ruddra Nov 19 '18 at 22:20
  • Adding the textes by cms (as childs) wouldn't work for me... because text1 should be rendered in a other div then text2 – Flo Nov 19 '18 at 22:22
  • @Flo not sure what you are asking, but you can look into this: https://stackoverflow.com/questions/11056844/django-how-to-get-data-connected-by-foreignkey-via-template about rendering ForeignKey in template. But still, you last comment is not actually in this scope of question. I hope you will acknowledge my contribution by accepting this answer based on current scope. If you need further assistance, please ask another question. We will gladly help you. – ruddra Nov 20 '18 at 04:20
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/184008/discussion-between-flo-and-ruddra). – Flo Nov 21 '18 at 07:08
0

I am not sure what you want to achieve. It looks like you could simplify this model to this

from django.db import models


class TextCollection(models.Model):
    text = models.CharField(max_length=100)
    text_font_weight = models.CharField(max_length=100)
    text_font_size = models.CharField(max_length=100)
    text_color = models.CharField(max_length=100)
    box = models.ForeignKey("Box", on_delete=models.CASCADE, related_name="textcollections") 


class Box(CMSPlugin):
    pass

This way you can use as much TextCollections as you want in a Box. If you also want a Text in multiple Boxes you could use ManyToManyField instead of ForeignKey. https://docs.djangoproject.com/en/2.1/topics/db/examples/many_to_many/

hda
  • 192
  • 1
  • 4