Disable option charField choices depending on another atribute (Django)












0















So, I have the following in my models.py:



UNITY_CHOICES = (
('g', 'Gram(s)'),
('kg', 'Kilogram(s)'),
('l', 'Liter(s)'),
('cl', 'Centiliter(s)'),
)
class Recipe_Ingredient(models.Model):
recipe = models.ForeignKey(Recipe, on_delete=models.CASCADE)
ingredient = models.ForeignKey(Ingredient, on_delete=models.CASCADE)
quantity = models.FloatField()
cost = models.DecimalField(max_digits=20, decimal_places=2)

quantityUnit = models.CharField(
max_length=2,
choices=UNITY_CHOICES,
default=GRAM,
)
class Ingredient(models.Model):
name = models.CharField(max_length=200)
articleNumber = models.IntegerField(unique=True)
costPrice = models.DecimalField(max_digits=20, decimal_places=2)
costAmout = models.FloatField()
costUnity = models.CharField(
max_length=2,
choices=UNITY_CHOICES,
default=GRAM,
)


And I have a simple form to add this model to my database:



class Recipe_IngredientForm(forms.ModelForm):
class Meta:
model = Recipe_Ingredient
fields = ('quantity', 'quantityUnit', 'ingredient')


So I was wondering if there's a way of filtering the quantityUnit available choices depending on the ingredient chosen.
I'll try to make it clear by an example: Let's say I choose to add Potato and the costUnity for Potato is 'g', then I want to make 'kg' and 'g' the only choices for quantityUnit. Is that a good example? I can try to think of something better if it's not clear enough.
Anyway, is it possible to do such thing? Thanks.



UPDATE:
forms.py:



class Recipe_IngredientForm(forms.ModelForm):

class Meta:
model = Recipe_Ingredient
fields = ('quantity', 'quantityUnit', 'ingredient')


template:



{% extends 'recipe/base.html' %}

{% block content %}
<h1>Add Ingredient to Recipe</h1>
<form method="POST" class="post-form">{% csrf_token %}
{{ form.as_p }}
<button type="submit" class="save btn btn-default">Save</button>
</form>
{% endblock %}


Rendered HTML:



<h1>Add Ingredient to Recipe</h1>
<form method="POST" class="post-form"><input type="hidden" name="csrfmiddlewaretoken" value="tXVR3NM1R4TBwOJArWWClL71r8S5C18gWKz9mKK42wlEamf6NcBjzrieF5dQBOaO">
<p>
<label for="id_quantity">Quantity:</label>
<input type="number" name="quantity" required="" id="id_quantity" step="any">
</p>
<p>
<label for="id_quantityUnit">QuantityUnit:</label>
<select name="quantityUnit" id="id_quantityUnit">
<option value="g" selected="">Gram(s)</option>
<option value="kg">Kilogram(s)</option>
<option value="l">Liter(s)</option>
<option value="cl">Centiliter(s)</option>
</select>
</p>
<p>
<label for="id_ingredient">Ingredient:</label> <select name="ingredient" required="" id="id_ingredient">
<option value="" selected="">---------</option>
<option value="1">Carrot</option>
<option value="2">Potato</option>
<option value="3">Water</option>
<option value="4">Juice</option>

</select>
</p>
<button type="submit" class="save btn btn-default">Save</button>
</form>









share|improve this question

























  • Yes, very possible with Javascrip/jQuery in your template. Here's an answer to a somewhat similar question. You would have to play around with your option elements

    – robotHamster
    Nov 20 '18 at 4:00











  • I don't think its possible unless you use any JQuery or other Javascripts to render them dynamically.

    – ruddra
    Nov 20 '18 at 4:22











  • @robotHamster So I would have to place a script tag at the bottom of the template where my form is being called. But how would I be able to get selected ingredient and the costUnity attribute of this selected ingredient?

    – Luc
    Nov 20 '18 at 10:31











  • You can use jQuery AJAX calls to update the information from your database

    – robotHamster
    Nov 20 '18 at 18:04











  • @robotHamster I have added the script at the bottom and triggered on change of the select field, but I still don't know how I can get the attribute costUnity from the selected ingredient. Can you help me out? Maybe with a simple example?

    – Luc
    Nov 20 '18 at 23:17
















0















So, I have the following in my models.py:



UNITY_CHOICES = (
('g', 'Gram(s)'),
('kg', 'Kilogram(s)'),
('l', 'Liter(s)'),
('cl', 'Centiliter(s)'),
)
class Recipe_Ingredient(models.Model):
recipe = models.ForeignKey(Recipe, on_delete=models.CASCADE)
ingredient = models.ForeignKey(Ingredient, on_delete=models.CASCADE)
quantity = models.FloatField()
cost = models.DecimalField(max_digits=20, decimal_places=2)

quantityUnit = models.CharField(
max_length=2,
choices=UNITY_CHOICES,
default=GRAM,
)
class Ingredient(models.Model):
name = models.CharField(max_length=200)
articleNumber = models.IntegerField(unique=True)
costPrice = models.DecimalField(max_digits=20, decimal_places=2)
costAmout = models.FloatField()
costUnity = models.CharField(
max_length=2,
choices=UNITY_CHOICES,
default=GRAM,
)


And I have a simple form to add this model to my database:



class Recipe_IngredientForm(forms.ModelForm):
class Meta:
model = Recipe_Ingredient
fields = ('quantity', 'quantityUnit', 'ingredient')


So I was wondering if there's a way of filtering the quantityUnit available choices depending on the ingredient chosen.
I'll try to make it clear by an example: Let's say I choose to add Potato and the costUnity for Potato is 'g', then I want to make 'kg' and 'g' the only choices for quantityUnit. Is that a good example? I can try to think of something better if it's not clear enough.
Anyway, is it possible to do such thing? Thanks.



UPDATE:
forms.py:



class Recipe_IngredientForm(forms.ModelForm):

class Meta:
model = Recipe_Ingredient
fields = ('quantity', 'quantityUnit', 'ingredient')


template:



{% extends 'recipe/base.html' %}

{% block content %}
<h1>Add Ingredient to Recipe</h1>
<form method="POST" class="post-form">{% csrf_token %}
{{ form.as_p }}
<button type="submit" class="save btn btn-default">Save</button>
</form>
{% endblock %}


Rendered HTML:



<h1>Add Ingredient to Recipe</h1>
<form method="POST" class="post-form"><input type="hidden" name="csrfmiddlewaretoken" value="tXVR3NM1R4TBwOJArWWClL71r8S5C18gWKz9mKK42wlEamf6NcBjzrieF5dQBOaO">
<p>
<label for="id_quantity">Quantity:</label>
<input type="number" name="quantity" required="" id="id_quantity" step="any">
</p>
<p>
<label for="id_quantityUnit">QuantityUnit:</label>
<select name="quantityUnit" id="id_quantityUnit">
<option value="g" selected="">Gram(s)</option>
<option value="kg">Kilogram(s)</option>
<option value="l">Liter(s)</option>
<option value="cl">Centiliter(s)</option>
</select>
</p>
<p>
<label for="id_ingredient">Ingredient:</label> <select name="ingredient" required="" id="id_ingredient">
<option value="" selected="">---------</option>
<option value="1">Carrot</option>
<option value="2">Potato</option>
<option value="3">Water</option>
<option value="4">Juice</option>

</select>
</p>
<button type="submit" class="save btn btn-default">Save</button>
</form>









share|improve this question

























  • Yes, very possible with Javascrip/jQuery in your template. Here's an answer to a somewhat similar question. You would have to play around with your option elements

    – robotHamster
    Nov 20 '18 at 4:00











  • I don't think its possible unless you use any JQuery or other Javascripts to render them dynamically.

    – ruddra
    Nov 20 '18 at 4:22











  • @robotHamster So I would have to place a script tag at the bottom of the template where my form is being called. But how would I be able to get selected ingredient and the costUnity attribute of this selected ingredient?

    – Luc
    Nov 20 '18 at 10:31











  • You can use jQuery AJAX calls to update the information from your database

    – robotHamster
    Nov 20 '18 at 18:04











  • @robotHamster I have added the script at the bottom and triggered on change of the select field, but I still don't know how I can get the attribute costUnity from the selected ingredient. Can you help me out? Maybe with a simple example?

    – Luc
    Nov 20 '18 at 23:17














0












0








0








So, I have the following in my models.py:



UNITY_CHOICES = (
('g', 'Gram(s)'),
('kg', 'Kilogram(s)'),
('l', 'Liter(s)'),
('cl', 'Centiliter(s)'),
)
class Recipe_Ingredient(models.Model):
recipe = models.ForeignKey(Recipe, on_delete=models.CASCADE)
ingredient = models.ForeignKey(Ingredient, on_delete=models.CASCADE)
quantity = models.FloatField()
cost = models.DecimalField(max_digits=20, decimal_places=2)

quantityUnit = models.CharField(
max_length=2,
choices=UNITY_CHOICES,
default=GRAM,
)
class Ingredient(models.Model):
name = models.CharField(max_length=200)
articleNumber = models.IntegerField(unique=True)
costPrice = models.DecimalField(max_digits=20, decimal_places=2)
costAmout = models.FloatField()
costUnity = models.CharField(
max_length=2,
choices=UNITY_CHOICES,
default=GRAM,
)


And I have a simple form to add this model to my database:



class Recipe_IngredientForm(forms.ModelForm):
class Meta:
model = Recipe_Ingredient
fields = ('quantity', 'quantityUnit', 'ingredient')


So I was wondering if there's a way of filtering the quantityUnit available choices depending on the ingredient chosen.
I'll try to make it clear by an example: Let's say I choose to add Potato and the costUnity for Potato is 'g', then I want to make 'kg' and 'g' the only choices for quantityUnit. Is that a good example? I can try to think of something better if it's not clear enough.
Anyway, is it possible to do such thing? Thanks.



UPDATE:
forms.py:



class Recipe_IngredientForm(forms.ModelForm):

class Meta:
model = Recipe_Ingredient
fields = ('quantity', 'quantityUnit', 'ingredient')


template:



{% extends 'recipe/base.html' %}

{% block content %}
<h1>Add Ingredient to Recipe</h1>
<form method="POST" class="post-form">{% csrf_token %}
{{ form.as_p }}
<button type="submit" class="save btn btn-default">Save</button>
</form>
{% endblock %}


Rendered HTML:



<h1>Add Ingredient to Recipe</h1>
<form method="POST" class="post-form"><input type="hidden" name="csrfmiddlewaretoken" value="tXVR3NM1R4TBwOJArWWClL71r8S5C18gWKz9mKK42wlEamf6NcBjzrieF5dQBOaO">
<p>
<label for="id_quantity">Quantity:</label>
<input type="number" name="quantity" required="" id="id_quantity" step="any">
</p>
<p>
<label for="id_quantityUnit">QuantityUnit:</label>
<select name="quantityUnit" id="id_quantityUnit">
<option value="g" selected="">Gram(s)</option>
<option value="kg">Kilogram(s)</option>
<option value="l">Liter(s)</option>
<option value="cl">Centiliter(s)</option>
</select>
</p>
<p>
<label for="id_ingredient">Ingredient:</label> <select name="ingredient" required="" id="id_ingredient">
<option value="" selected="">---------</option>
<option value="1">Carrot</option>
<option value="2">Potato</option>
<option value="3">Water</option>
<option value="4">Juice</option>

</select>
</p>
<button type="submit" class="save btn btn-default">Save</button>
</form>









share|improve this question
















So, I have the following in my models.py:



UNITY_CHOICES = (
('g', 'Gram(s)'),
('kg', 'Kilogram(s)'),
('l', 'Liter(s)'),
('cl', 'Centiliter(s)'),
)
class Recipe_Ingredient(models.Model):
recipe = models.ForeignKey(Recipe, on_delete=models.CASCADE)
ingredient = models.ForeignKey(Ingredient, on_delete=models.CASCADE)
quantity = models.FloatField()
cost = models.DecimalField(max_digits=20, decimal_places=2)

quantityUnit = models.CharField(
max_length=2,
choices=UNITY_CHOICES,
default=GRAM,
)
class Ingredient(models.Model):
name = models.CharField(max_length=200)
articleNumber = models.IntegerField(unique=True)
costPrice = models.DecimalField(max_digits=20, decimal_places=2)
costAmout = models.FloatField()
costUnity = models.CharField(
max_length=2,
choices=UNITY_CHOICES,
default=GRAM,
)


And I have a simple form to add this model to my database:



class Recipe_IngredientForm(forms.ModelForm):
class Meta:
model = Recipe_Ingredient
fields = ('quantity', 'quantityUnit', 'ingredient')


So I was wondering if there's a way of filtering the quantityUnit available choices depending on the ingredient chosen.
I'll try to make it clear by an example: Let's say I choose to add Potato and the costUnity for Potato is 'g', then I want to make 'kg' and 'g' the only choices for quantityUnit. Is that a good example? I can try to think of something better if it's not clear enough.
Anyway, is it possible to do such thing? Thanks.



UPDATE:
forms.py:



class Recipe_IngredientForm(forms.ModelForm):

class Meta:
model = Recipe_Ingredient
fields = ('quantity', 'quantityUnit', 'ingredient')


template:



{% extends 'recipe/base.html' %}

{% block content %}
<h1>Add Ingredient to Recipe</h1>
<form method="POST" class="post-form">{% csrf_token %}
{{ form.as_p }}
<button type="submit" class="save btn btn-default">Save</button>
</form>
{% endblock %}


Rendered HTML:



<h1>Add Ingredient to Recipe</h1>
<form method="POST" class="post-form"><input type="hidden" name="csrfmiddlewaretoken" value="tXVR3NM1R4TBwOJArWWClL71r8S5C18gWKz9mKK42wlEamf6NcBjzrieF5dQBOaO">
<p>
<label for="id_quantity">Quantity:</label>
<input type="number" name="quantity" required="" id="id_quantity" step="any">
</p>
<p>
<label for="id_quantityUnit">QuantityUnit:</label>
<select name="quantityUnit" id="id_quantityUnit">
<option value="g" selected="">Gram(s)</option>
<option value="kg">Kilogram(s)</option>
<option value="l">Liter(s)</option>
<option value="cl">Centiliter(s)</option>
</select>
</p>
<p>
<label for="id_ingredient">Ingredient:</label> <select name="ingredient" required="" id="id_ingredient">
<option value="" selected="">---------</option>
<option value="1">Carrot</option>
<option value="2">Potato</option>
<option value="3">Water</option>
<option value="4">Juice</option>

</select>
</p>
<button type="submit" class="save btn btn-default">Save</button>
</form>






python django django-models django-forms






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 '18 at 18:24







Luc

















asked Nov 20 '18 at 3:24









LucLuc

338




338













  • Yes, very possible with Javascrip/jQuery in your template. Here's an answer to a somewhat similar question. You would have to play around with your option elements

    – robotHamster
    Nov 20 '18 at 4:00











  • I don't think its possible unless you use any JQuery or other Javascripts to render them dynamically.

    – ruddra
    Nov 20 '18 at 4:22











  • @robotHamster So I would have to place a script tag at the bottom of the template where my form is being called. But how would I be able to get selected ingredient and the costUnity attribute of this selected ingredient?

    – Luc
    Nov 20 '18 at 10:31











  • You can use jQuery AJAX calls to update the information from your database

    – robotHamster
    Nov 20 '18 at 18:04











  • @robotHamster I have added the script at the bottom and triggered on change of the select field, but I still don't know how I can get the attribute costUnity from the selected ingredient. Can you help me out? Maybe with a simple example?

    – Luc
    Nov 20 '18 at 23:17



















  • Yes, very possible with Javascrip/jQuery in your template. Here's an answer to a somewhat similar question. You would have to play around with your option elements

    – robotHamster
    Nov 20 '18 at 4:00











  • I don't think its possible unless you use any JQuery or other Javascripts to render them dynamically.

    – ruddra
    Nov 20 '18 at 4:22











  • @robotHamster So I would have to place a script tag at the bottom of the template where my form is being called. But how would I be able to get selected ingredient and the costUnity attribute of this selected ingredient?

    – Luc
    Nov 20 '18 at 10:31











  • You can use jQuery AJAX calls to update the information from your database

    – robotHamster
    Nov 20 '18 at 18:04











  • @robotHamster I have added the script at the bottom and triggered on change of the select field, but I still don't know how I can get the attribute costUnity from the selected ingredient. Can you help me out? Maybe with a simple example?

    – Luc
    Nov 20 '18 at 23:17

















Yes, very possible with Javascrip/jQuery in your template. Here's an answer to a somewhat similar question. You would have to play around with your option elements

– robotHamster
Nov 20 '18 at 4:00





Yes, very possible with Javascrip/jQuery in your template. Here's an answer to a somewhat similar question. You would have to play around with your option elements

– robotHamster
Nov 20 '18 at 4:00













I don't think its possible unless you use any JQuery or other Javascripts to render them dynamically.

– ruddra
Nov 20 '18 at 4:22





I don't think its possible unless you use any JQuery or other Javascripts to render them dynamically.

– ruddra
Nov 20 '18 at 4:22













@robotHamster So I would have to place a script tag at the bottom of the template where my form is being called. But how would I be able to get selected ingredient and the costUnity attribute of this selected ingredient?

– Luc
Nov 20 '18 at 10:31





@robotHamster So I would have to place a script tag at the bottom of the template where my form is being called. But how would I be able to get selected ingredient and the costUnity attribute of this selected ingredient?

– Luc
Nov 20 '18 at 10:31













You can use jQuery AJAX calls to update the information from your database

– robotHamster
Nov 20 '18 at 18:04





You can use jQuery AJAX calls to update the information from your database

– robotHamster
Nov 20 '18 at 18:04













@robotHamster I have added the script at the bottom and triggered on change of the select field, but I still don't know how I can get the attribute costUnity from the selected ingredient. Can you help me out? Maybe with a simple example?

– Luc
Nov 20 '18 at 23:17





@robotHamster I have added the script at the bottom and triggered on change of the select field, but I still don't know how I can get the attribute costUnity from the selected ingredient. Can you help me out? Maybe with a simple example?

– Luc
Nov 20 '18 at 23:17












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%2f53385765%2fdisable-option-charfield-choices-depending-on-another-atribute-django%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%2f53385765%2fdisable-option-charfield-choices-depending-on-another-atribute-django%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







Popular posts from this blog

How to change which sound is reproduced for terminal bell?

Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents

Can I use Tabulator js library in my java Spring + Thymeleaf project?