Add An Object Along With It's Related Objects In The Same Form In Django Admin
I have these models: class Gallery(models.Model): HeadImage = models.ImageField(upload_to='gallery') class Image(models.Model): Image = models.ImageField(upload_to='gallery
Solution 1:
As I said in the comment above, you're trying to do too much in the Django admin.
Looking at your other question, you are familar with the tools you need to write your own view. Define model forms for your models. Put them in the same form tag in your template. If the form is valid, save with commit=False
then fix up the foreign keys.
Here's a skeleton view for adding an Agency
and a Gallery
together. You could easily add a formset of images at the same time.
def add_agency(request):
if request.method == "POST":
agency_form = AgencyForm(data=request.POST, prefix="agency")
gallery_form = GalleryForm(data=request.POST, prefix="gallery")
if agency_form.is_valid() and gallery_form.is_valid():
gallery = gallery_form.save()
agency = agency_form.save(commit=False)
agency.gallery = gallery
agency.save()
return HttpResponseRedirect(next_url)
else:
# left as an exercise
If you want a two step form, I recommend you look at formwizard. It's a separate app for Django <=1.3.X], and included in Django >=1.4.
Post a Comment for "Add An Object Along With It's Related Objects In The Same Form In Django Admin"