Django: Multiple models on the same table










1















I am building a recipe/cookbook application (something like cookpad or allrecipes) for a personal project.
I have a model for image like so:



class Image(Model):
type = (("R", "Recipe"), ("P", "Profile"),
("E", "Embedded"), ("B", "Base"))
name = models.CharField(max_length=55)
type = models.CharField(choices=type, max_length=1, default="B")
image = VersatileImageField(
upload_to=FileNameGenerator(prefix="images"),
placeholder_image=OnDiscPlaceholderImage(
path=join(settings.MEDIA_ROOT, "images", "default.jpg")
),
null=True,
blank=True,
)


I want to use one table only to store all image. But store it to different place for each different type.



Example:



  • Base image would be at images/xxx.jpg

  • Profile's image at profiles/xxxx.jpg

  • Recipe's image at recipes/xxx.jpg

  • etc..

The only way I know how, is to make it an abstract model and extend it. But it would create many table (ProfileImage, RecipeImage, etc..)



If I use proxy I cannot change/override the behaviour of image field.



Is there a a way to extend models but save it to the same table in database?










share|improve this question
























  • check this stackoverflow.com/questions/5135556/dynamic-file-path-in-django

    – Selim Yılmaz
    Nov 15 '18 at 6:39






  • 1





    You don't need separate models here. You just need a dynamic upload_to parameter - which you already seem to have, with FileNameGenerator. Why can't you extend that to use different directories as well as filenames?

    – Daniel Roseman
    Nov 15 '18 at 8:36















1















I am building a recipe/cookbook application (something like cookpad or allrecipes) for a personal project.
I have a model for image like so:



class Image(Model):
type = (("R", "Recipe"), ("P", "Profile"),
("E", "Embedded"), ("B", "Base"))
name = models.CharField(max_length=55)
type = models.CharField(choices=type, max_length=1, default="B")
image = VersatileImageField(
upload_to=FileNameGenerator(prefix="images"),
placeholder_image=OnDiscPlaceholderImage(
path=join(settings.MEDIA_ROOT, "images", "default.jpg")
),
null=True,
blank=True,
)


I want to use one table only to store all image. But store it to different place for each different type.



Example:



  • Base image would be at images/xxx.jpg

  • Profile's image at profiles/xxxx.jpg

  • Recipe's image at recipes/xxx.jpg

  • etc..

The only way I know how, is to make it an abstract model and extend it. But it would create many table (ProfileImage, RecipeImage, etc..)



If I use proxy I cannot change/override the behaviour of image field.



Is there a a way to extend models but save it to the same table in database?










share|improve this question
























  • check this stackoverflow.com/questions/5135556/dynamic-file-path-in-django

    – Selim Yılmaz
    Nov 15 '18 at 6:39






  • 1





    You don't need separate models here. You just need a dynamic upload_to parameter - which you already seem to have, with FileNameGenerator. Why can't you extend that to use different directories as well as filenames?

    – Daniel Roseman
    Nov 15 '18 at 8:36













1












1








1


0






I am building a recipe/cookbook application (something like cookpad or allrecipes) for a personal project.
I have a model for image like so:



class Image(Model):
type = (("R", "Recipe"), ("P", "Profile"),
("E", "Embedded"), ("B", "Base"))
name = models.CharField(max_length=55)
type = models.CharField(choices=type, max_length=1, default="B")
image = VersatileImageField(
upload_to=FileNameGenerator(prefix="images"),
placeholder_image=OnDiscPlaceholderImage(
path=join(settings.MEDIA_ROOT, "images", "default.jpg")
),
null=True,
blank=True,
)


I want to use one table only to store all image. But store it to different place for each different type.



Example:



  • Base image would be at images/xxx.jpg

  • Profile's image at profiles/xxxx.jpg

  • Recipe's image at recipes/xxx.jpg

  • etc..

The only way I know how, is to make it an abstract model and extend it. But it would create many table (ProfileImage, RecipeImage, etc..)



If I use proxy I cannot change/override the behaviour of image field.



Is there a a way to extend models but save it to the same table in database?










share|improve this question
















I am building a recipe/cookbook application (something like cookpad or allrecipes) for a personal project.
I have a model for image like so:



class Image(Model):
type = (("R", "Recipe"), ("P", "Profile"),
("E", "Embedded"), ("B", "Base"))
name = models.CharField(max_length=55)
type = models.CharField(choices=type, max_length=1, default="B")
image = VersatileImageField(
upload_to=FileNameGenerator(prefix="images"),
placeholder_image=OnDiscPlaceholderImage(
path=join(settings.MEDIA_ROOT, "images", "default.jpg")
),
null=True,
blank=True,
)


I want to use one table only to store all image. But store it to different place for each different type.



Example:



  • Base image would be at images/xxx.jpg

  • Profile's image at profiles/xxxx.jpg

  • Recipe's image at recipes/xxx.jpg

  • etc..

The only way I know how, is to make it an abstract model and extend it. But it would create many table (ProfileImage, RecipeImage, etc..)



If I use proxy I cannot change/override the behaviour of image field.



Is there a a way to extend models but save it to the same table in database?







python django django-models






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 6:27







flemadap

















asked Nov 15 '18 at 6:20









flemadapflemadap

84




84












  • check this stackoverflow.com/questions/5135556/dynamic-file-path-in-django

    – Selim Yılmaz
    Nov 15 '18 at 6:39






  • 1





    You don't need separate models here. You just need a dynamic upload_to parameter - which you already seem to have, with FileNameGenerator. Why can't you extend that to use different directories as well as filenames?

    – Daniel Roseman
    Nov 15 '18 at 8:36

















  • check this stackoverflow.com/questions/5135556/dynamic-file-path-in-django

    – Selim Yılmaz
    Nov 15 '18 at 6:39






  • 1





    You don't need separate models here. You just need a dynamic upload_to parameter - which you already seem to have, with FileNameGenerator. Why can't you extend that to use different directories as well as filenames?

    – Daniel Roseman
    Nov 15 '18 at 8:36
















check this stackoverflow.com/questions/5135556/dynamic-file-path-in-django

– Selim Yılmaz
Nov 15 '18 at 6:39





check this stackoverflow.com/questions/5135556/dynamic-file-path-in-django

– Selim Yılmaz
Nov 15 '18 at 6:39




1




1





You don't need separate models here. You just need a dynamic upload_to parameter - which you already seem to have, with FileNameGenerator. Why can't you extend that to use different directories as well as filenames?

– Daniel Roseman
Nov 15 '18 at 8:36





You don't need separate models here. You just need a dynamic upload_to parameter - which you already seem to have, with FileNameGenerator. Why can't you extend that to use different directories as well as filenames?

– Daniel Roseman
Nov 15 '18 at 8:36












0






active

oldest

votes











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
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53313538%2fdjango-multiple-models-on-the-same-table%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes















draft saved

draft discarded
















































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.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53313538%2fdjango-multiple-models-on-the-same-table%23new-answer', 'question_page');

);

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







這個網誌中的熱門文章

Barbados

How to read a connectionString WITH PROVIDER in .NET Core?

Node.js Script on GitHub Pages or Amazon S3