django collection of model fields
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 |
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 |
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 '18 at 17:53


FloFlo
167521
167521
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
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 |
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 '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
|
show 20 more comments
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
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
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
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 |
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 |
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 '18 at 18:20
answered Nov 19 '18 at 18:07
hdahda
7214
7214
add a comment |
add a comment |
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 '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
|
show 20 more comments
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 '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
|
show 20 more comments
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 '18 at 21:15
answered Nov 19 '18 at 18:06
ruddraruddra
12.2k32648
12.2k32648
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
|
show 20 more comments
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
Thanks for your answer! But than if i want to include that taxt in multiple other classes?
– Flo
Nov 19 '18 at 18:11
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
@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
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
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
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
|
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