django collection of model fields
up vote
0
down vote
favorite
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...
python django
add a comment |
up vote
0
down vote
favorite
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...
python django
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
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...
python django
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...
python django
python django
asked Nov 19 at 17:53
Flo
162521
162521
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
up vote
0
down vote
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/
add a comment |
up vote
0
down vote
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)
Thanks for your answer! But than if i want to include that taxt in multiple other classes?
– Flo
Nov 19 at 18:11
@Flo I have added an update section, please have a look.
– ruddra
Nov 19 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 at 18:26
sorry, i did not get that, what do you mean by new box screen? is it another model?
– ruddra
Nov 19 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 at 18:29
|
show 20 more comments
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
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/
add a comment |
up vote
0
down vote
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/
add a comment |
up vote
0
down vote
up vote
0
down vote
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/
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/
edited Nov 19 at 18:20
answered Nov 19 at 18:07
hda
2912
2912
add a comment |
add a comment |
up vote
0
down vote
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)
Thanks for your answer! But than if i want to include that taxt in multiple other classes?
– Flo
Nov 19 at 18:11
@Flo I have added an update section, please have a look.
– ruddra
Nov 19 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 at 18:26
sorry, i did not get that, what do you mean by new box screen? is it another model?
– ruddra
Nov 19 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 at 18:29
|
show 20 more comments
up vote
0
down vote
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)
Thanks for your answer! But than if i want to include that taxt in multiple other classes?
– Flo
Nov 19 at 18:11
@Flo I have added an update section, please have a look.
– ruddra
Nov 19 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 at 18:26
sorry, i did not get that, what do you mean by new box screen? is it another model?
– ruddra
Nov 19 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 at 18:29
|
show 20 more comments
up vote
0
down vote
up vote
0
down vote
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)
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)
edited Nov 19 at 21:15
answered Nov 19 at 18:06
ruddra
9,41832547
9,41832547
Thanks for your answer! But than if i want to include that taxt in multiple other classes?
– Flo
Nov 19 at 18:11
@Flo I have added an update section, please have a look.
– ruddra
Nov 19 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 at 18:26
sorry, i did not get that, what do you mean by new box screen? is it another model?
– ruddra
Nov 19 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 at 18:29
|
show 20 more comments
Thanks for your answer! But than if i want to include that taxt in multiple other classes?
– Flo
Nov 19 at 18:11
@Flo I have added an update section, please have a look.
– ruddra
Nov 19 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 at 18:26
sorry, i did not get that, what do you mean by new box screen? is it another model?
– ruddra
Nov 19 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 at 18:29
Thanks for your answer! But than if i want to include that taxt in multiple other classes?
– Flo
Nov 19 at 18:11
Thanks for your answer! But than if i want to include that taxt in multiple other classes?
– Flo
Nov 19 at 18:11
@Flo I have added an update section, please have a look.
– ruddra
Nov 19 at 18:16
@Flo I have added an update section, please have a look.
– ruddra
Nov 19 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 at 18:26
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 at 18:26
sorry, i did not get that, what do you mean by new box screen? is it another model?
– ruddra
Nov 19 at 18:28
sorry, i did not get that, what do you mean by new box screen? is it another model?
– ruddra
Nov 19 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 at 18:29
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 at 18:29
|
show 20 more comments
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53380187%2fdjango-collection-of-model-fields%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown