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 7779905
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T18:46:11+00:00 2026-06-01T18:46:11+00:00

I am working on a cookbook website using django and have run into a

  • 0

I am working on a cookbook website using django and have run into a problem with a foreign key field in my form

the problem is that when i create my recipe i need to have a foreign key pointing to the cookbook that created this recipe but I don’t want the user creating the recipe to see the original_cookbook field (they shouldn’t have to)

I believe I need to use a widget (HiddenInput) but am I getting confused with the examples given on other sites. Also my friend mentioned something about setting an initial value in the original_cookbook view

tl;dr: I want to point the fk to the users cookbook while keeping the original_cookbook field hidden.

relevant code:

form:

class RecipeForm(forms.ModelForm):

    class Meta:
        model = Recipe

model:

class Recipe(models.Model):
    def __unicode__(self):
        return self.name
    original_cookbook = models.ForeignKey(Cookbook)
    #cookbooks = models.ManyToManyField('Cookbook', related_name = 'recipes')
    name = models.CharField(max_length=200)
    author = models.CharField(max_length= 100)
    picture = models.ImageField(upload_to = 'Downloads', blank=True)
    pub_date = models.DateTimeField('date published', auto_now_add=True, blank=True)
    ingredients = models.TextField()
    steps = models.TextField()
    prep_time = models.IntegerField()
    TYPE_CHOICES= (
    ('SW', 'Sandwich'),
    ('AP', 'Appetizers'),
    ('SD', 'Sauces and Dressings'),
    ('SS', 'Soups and Salads'),
    ('VG', 'Vegetables'),
    ('RG', 'Rice, Grains and Beans'),
    ('PA', 'Pasta'),
    ('BR', 'Breakfast'),
    ('MT', 'Meat'),
    ('SF', 'Seafood'),
    ('BP', 'Bread and Pizza'),
    ('DT', 'Desserts'),
    )
    type = models.CharField(max_length = 2, choices=TYPE_CHOICES)

    def index_queryset(self):
        return self.objects.all()

view:

def createrecipe(request):
    if not request.user.is_authenticated():
        return HttpResponseRedirect('/index/')
    else:
        if request.method == 'POST':
            form = RecipeForm(request.POST)
            if form.is_valid():
                recipe = form.save()
                user = request.user
                cookbooks = user.cookbooks
                cookbook = cookbooks.all()[0]
                cookbook.recipes.add(recipe)
                return HttpResponseRedirect('/account')
        else:
            form = RecipeForm()

        return render_to_response('cookbook/createrecipe.html',
                                    {'form':form},
                              context_instance=RequestContext(request))
  • 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-06-01T18:46:13+00:00Added an answer on June 1, 2026 at 6:46 pm

    Add exclude = ('original_cookbook',) to your form’s Meta class.

    Then, in your if form.is_valid() code, do something like:

    ....
    recipe = form.save(commit=False)
    recipe.original_cookbook = whatever_that_is
    recipe.save()
    ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Working on a website that has Employee and Branch entities, using a database table
Working on a movie website and would love to find an API that I
I'm new to cakephp so have just been working through the cookbook and am
I have problem with uploading files. I followed these instructions: http://symfony.com/doc/current/cookbook/doctrine/file_uploads.html and everything is
I'm working on a project that uses OpenGL 4.0 shaders. I have to supply
I am working my way through the Android Developer's Cookbook. I have entered in
We are working on integrating two different applications that run simultaneously and share data.
Working with C and Win32, I have a problem where my program freezes instead
Working on a bunch of forms at the moment and I'm finding that I
Working on my first Rails project, I used Ryan Bates' Nifty-Generators to create the

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.