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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T17:16:05+00:00 2026-05-16T17:16:05+00:00

I have a complex django object, which has properties of other class types. This

  • 0

I have a complex django object, which has properties of other class types. This gets like this:

class Order:
   contractor - type Person
   some other fields....

In my form I’d like to be able to either choose existing Person object from the dropdown or add a new one with a form. I’ve managed to create forms and appropriate workflow, but the problem is with saving the Order itself, I simply can’t get the id of a saved Person instance. I’m doing sth like this:

def make_order(request):
  if request.method == 'POST':
    parameters = copy.copy(request.POST)
    contractor_form = ContractorForm(parameters)
    if contractor_form.is_valid():
      contractor_form.save()
      parameters['contractor'] = ???
    form = OrderForm(parameters)
    if form.is_valid():
      form.save() 
      return HttpResponseRedirect('/orders/')
  else:
    form = OrderForm()
    contractor_form = ContractorForm()

  return render_to_response('orders/make_order.html', {'order_form' : form, 'contractor_form' : contractor_form})

So, if POST request reaches this method I first check if ContractorForm have been filled – I assume that if the form is valid, it is meant to be used. If yes, than I save it and would like to assign the saved object’s database id to appropriate field for OrderForm to find it.

All my forms are ModelForms.

The questions are:

  1. Are there better ways to do this? (choose from dropdown or add in place) – better or more pythonic 😉
  2. How can I get saved objects id when using ModelForms?

Edited

My ContractorForm is:

class ContractorForm(ModelForm):
  class Meta:
    model = Contractor

Nothing fancy.

  • 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-16T17:16:06+00:00Added an answer on May 16, 2026 at 5:16 pm

    save() should return the newly created instance.

    if contractor_form.is_valid():
      instance = contractor_form.save()
      parameters['contractor'] = instance
    

    where id would be instance.id, or even better instance.pk.

    pk vs. id:

    Regardless of whether you define a
    primary key field yourself, or let
    Django supply one for you, each model
    will have a property called pk. It
    behaves like a normal attribute on the
    model, but is actually an alias for
    whichever attribute is the primary key
    field for the model. You can read and
    set this value, just as you would for
    any other attribute, and it will
    update the correct field in the model.

    Follow-up on comment:

    Well it does work by default, so there must be something else wrong.

    models.py

    class Category(models.Model):
        name = models.CharField(max_length=70)
        slug = models.SlugField()
    

    forms.py

    from django import forms
    from models import Category
    
    class MyModelForm(forms.ModelForm):
        class Meta:
            model = Category
    

    Test in shell:

    In [3]: from katalog.forms import MyModelForm
    In [4]: data = {'name':'Test', 'slug':'test'}
    In [5]: form = MyModelForm(data)
    In [6]: instance = form.save()
    In [7]: instance
    Out[7]: <Category: Test>
    In [8]: instance.id
    Out[8]: 5L
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using WCF and REST, and I have complex types, which are working
I have a fairly complex Django project which makes it hard/impossible to use fixtures
I have several complex data structures like Map< A, Set< B > > Set<
I have some complex regular expressions which I need to comment for readability and
I have a complex type defined which doesn't currently contain any minOccurs restrictions. When
I have written a simple feedback application in django. It's not particulairly complex, basically
Following the Heroku tutorial but I have already created a rather complex Django app
I have a problem with django's MVC. I understand that this is not the
I have a fairly complex report generating work, which needs to be generated spaning
I have a ModelChoiceField in django forms: customField = forms.ModelChoiceField(queryset=MyModel.objects.filter(type=sometype)) But this queryset returns

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.