Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6230977
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T09:50:58+00:00 2026-05-24T09:50:58+00:00

I have a question on ModelForms in Django. If I’m creating a form from

  • 0

I have a question on ModelForms in Django. If I’m creating a form from a model using ModelForms, then how the form fields will be linked to these M2M relationships ? what I mean is if I have :

Models.py

class Recipe(models.Model):
    title = models.CharField(max_length=100)
    category = models.ForeignKey(Category)
    cuisineType = models.ForeignKey(CuisineType)
    description = models.TextField()
    serving = models.CharField(max_length=100)
    cooking_time = models.TimeField()
    ingredients = models.ManyToManyField(RecipeIngredient)
    directions = models.TextField()
    image = models.OneToOneField(Image)
    added_at = models.DateTimeField(auto_now_add=True)
    last_update = models.DateTimeField(auto_now=True)
    added_by = models.ForeignKey(UserProfile, null=False)
    tags = models.ManyToManyField(Tag,blank=True)

class Category(models.Model):

    category = models.CharField(max_length=100)
    category_english = models.CharField(max_length=100)
    #slug = models.SlugField(prepopulate_from=('name_english',))
    slug = models.SlugField()
    parent = models.ForeignKey('self', blank=True, null=True, related_name='child')
    description = models.TextField(blank=True,help_text="Optional")
    class Meta:
        verbose_name_plural = 'Categories'
        #sort = ['category']

Forms.py

class RecipeForm(ModelForm):
    class Meta:
        model = Recipe
        exclude = ('added_at','last_update','added_by')

Views.py

def RecipeEditor(request, id=None):
    form = RecipeForm(request.POST or None,
                       instance=id and Recipe.objects.get(id=id))

    # Save new/edited Recipe
    if request.method == 'POST' and form.is_valid():
        form.save()
        return HttpResponseRedirect('/recipes/')     #TODO: write a url + add it to urls .. this is temp

    return render_to_response('adding_recipe_form.html',{'form':form})

then should I create 1 modelform for the 2 models related to each other as I did ? or a modelform for each model ? If I do one, how I’m going to exclude fields from the other model ? I’m a bit confused.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T09:51:00+00:00Added an answer on May 24, 2026 at 9:51 am

    1.Should I create 1 model form for the 2 models related to each other as I did ?
    No, you cant. Django use’s this list to do a model field to form field mapping. Related fields are shown as select/drop down box. These select/drop down box are populated with existing instances of the related field.

    2.Model form for each model?
    Its better to create a model form for each model.

    3.If I do one, how I’m going to exclude fields from the other model ?
    If you create a model form for each model then you can use exclude in their individual model forms.

    say for eg:

    class CategoryForm(ModelForm):
          class Meta:
                model = Category
                exclude = ('slug ')
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I using Django and a generic view django.views.generic.create_update.create_object I have a model form wich
I have a custom model fields, that can have 'chain' argument. from django.db import
I have a Django app with custom form fields, some of which have slow
I have 2 models in django, and im also using ModelForm, my question is
I'm new to python and to django so this question will probably be easy
following on from this question . I have the following: models.py VARIABLE_CHOICES = (
I have question will the click event or any other event run in document.ready()
I have question about throw. How will the throw work in the following code?
I have question here, i design multiview application using view base template. Page 1
I have question model which has many options. In my question controller new action

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.