#25023: Many to Many through - save new Data
--------------------------------------+--------------------
Reporter: trap12 | Owner: nobody
Type: Cleanup/optimization | Status: new
Component: Forms | Version: 1.8
Severity: Release blocker | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 0 | UI/UX: 0
--------------------------------------+--------------------
Hi,
I am new in Django and have problems with the M2M-Fields with a custom
join table (through). I want to to save new Data (no duplicate). But the
form shows me just the data in the db, which I can select.
I have no access to the through-table "RecipeIngredients" which links the
ingredient with the recipe and holds the quantity.
I want to have a dynamic field, where I can add also new ingredient, which
are not in the db. And I want to add the field quanity in the RecipeForm.
(problem: the quantity field is not in the model Recipe, it is in the
RecipeIngredients)
models.py:
{{{
class Ingredient(models.Model):
id = models.AutoField(primary_key=True)
name = models.CharField(max_length=200)
class Recipe(models.Model):
id = models.AutoField(primary_key=True)
recipename = models.CharField(max_length=200)
author = models.ForeignKey(Student, null=True)
pub_date = models.DateTimeField('date published', editable=False)
ingredients = models.ManyToManyField(Ingredient,
through='Recipeingredients')
description = models.CharField(max_length=20000)
class Recipeingredients(models.Model):
id = models.AutoField(primary_key=True)
ingredient = models.ForeignKey(Ingredient)
recipe = models.ForeignKey(Recipe)
quantity = models.CharField(max_length=30)
}}}
forms.py:
{{{
class RecipeForm(forms.ModelForm):
class Meta:
model = Recipe
exclude = ('id', 'author', 'pub_date')
}}}
views.py:
{{{
@login_required
def create(request):
if request.method == 'POST':
form = RecipeForm(user=request.user, data=request.POST)
if form.is_valid():
recipe = form.save()
return HttpResponseRedirect(recipe.get_absolute_url())
else:
form = RecipeForm()
return render(request, 'recipes/create.html', {'form': form}}
}}}
create.html:
{{{
<form action="{{ action_url }}" method="post" accept-charset="utf-8">
{{ form.as_p }}
{% csrf_token %}
<p><input type="submit" value="Save"/></p>
</form>
}}}
Like I said, I have no textfield to add new ones.
Do you know what I have to change?
thanks in advance!!!
regards,
trap123
--
Ticket URL: <https://code.djangoproject.com/ticket/25023>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/049.bd1b1fac6a944f9f56641d37def056fe%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.