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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T11:44:51+00:00 2026-06-01T11:44:51+00:00

I have the following model in a project: class CarAssignment(models.Model): leg = models.ForeignKey(Leg, null=True,

  • 0

I have the following model in a project:

class CarAssignment(models.Model):
    leg = models.ForeignKey(Leg, null=True, blank=True)
    driver = models.ForeignKey(Driver, null=True, blank=True)
    riders = models.ManyToManyField(Rider, null=True, blank=True)

I’m trying to create a page with a table where an admin user can edit the driver and riders items for all of the car assignments in a specific group. I can’t use a formset because I also need to add data from another model, and because the choices in each list come from a different subset of riders and drivers, those that belong to a specific group. Driver is a select list and Riders is a multiselect list.

I have successfully built the page and table to represent the data, with correctly working drop-down lists. However, I can’t figure out how to save the items. Here’s a specific problem scenario:

for row 1 in the table (representing CarAssignment object #1), I pick a new driver from the list in the select dropdown. I’m assigning custom name tags in the html to identify them, as follows:

<option selected name="select.driver.{{ car_assignment.id }}">{{ driver }}</option>

Then in the view, I’m trying to see if it matches anything, as follows:

        for car_assignment in CarAssignment.objects.filter(leg__in=legs):
            driver_tag = "select.driver." + str(car_assignment.id)
            if driver_tag in request.POST:
                car_assignment.driver = driver
                car_assignment.save()

The problem is that when it finds a matching driver tag, all it returns is a string of the First Name and Last Name. I can’t do the car_assignment.driver = driver because it’s trying to equate a driver object to a string.

I’m not sure how to do this. I guess I could parse out the first and last names and try to match them against the Driver model to get the Driver objects I need, but that seems inefficient and possibly error-prone.

Any help is appreciated (I’m a programming and Django newbie). Thanks.

  • 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-01T11:44:52+00:00Added an answer on June 1, 2026 at 11:44 am

    I’m not sure where your that driver field is coming from, but if it’s a String, you could then do another query to get the driver object like so:

    for car_assignment in CarAssignment.objects.filter(leg__in=legs):
        driver_tag = "select.driver." + str(car_assignment.id)
        if driver_tag in request.POST:
            # Get the driver object
            driver = Driver.objects.filter(name=driver_name)[0]
    
            car_assignment.driver = driver
            car_assignment.save()
    

    I’d also highly recommend that you read the Django tutorial Working with forms. The logic that you’re writing in the view should be moved to a form object. Your view can then instantiate the form and pass the post data. The form can then be validated and will handle saving your new objects. This is the best way to do this.

    The updated flow for your view would be simplified to something like this:

    def my_view(request):
        if request.method == 'POST':
            form = MyForm(request.POST)
            if form.is_valid():
                form.save()
    
                # Do something here like redirect the user
                # to acknowledge that the form was submitted successfully.
        else:
            form =  MyForm()
    
        return render_to_response('form.html', {
            'form': form,
        })
    

    By passing the form to your template you can generate the form doing nothing but this:

    {{ form.as_ul }}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a django model as following class Project(models.Model) name=models.CharField(max_length=200) class Application(models.Model) proj=models.ForeignKey(Project, null=True,
I have the following model structure: class Container(models.Model): pass class Generic(models.Model): name = models.CharacterField(unique=True)
I have the following model and it's form: class Project(models.Model) class ProjectForm(forms.ModelForm) class Meta:
I currently have the following models/toy.rb file in my RoR project: class ToysPurchased include
I have a following model: class Car(models.Model): make = models.CharField(max_length=40) mileage_limit = models.IntegerField() mileage
I have the following model and instance: class Bashable(models.Model): name = models.CharField(max_length=100) >>> foo
I have the following model: class User: name = models.CharField( max_length = 200 )
Let's assume we have following models: from django.db import models class Foo(models.Model): name =
Say I have two models in my Django project. class Project(models.Model): name = models.CharField(max_length=256)
I have the following models in my ASP.NET MVC 3 project: public class Task

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.