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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T23:41:24+00:00 2026-05-31T23:41:24+00:00

I’m not very good with Django’s form . Here is Order from models.py class

  • 0

I’m not very good with Django’s form.

Here is Order from models.py

class Order( models.Model ) :
    def __unicode__( self ) :
        return unicode( self.pk )

    def get_absolute_url( self ) :
        return reverse( 'orders_detail', args = [ self.pk ] )

    STATUS_CHOICES = (
        ( 'p', 'pending'  ),
        ( 'a', 'approved' ),
        ( 'r', 'rejected' ),
        ( 'c', 'closed'   ),
        ( 'l', 'locked'   ),
    )

    WORK_TYPE_CHOICES = (
        ( 'hc', 'Heating and cooling' ),
        ( 'el', 'Electrical'          ),
        ( 'pl', 'Plumbing'            ),
        ( 'ap', 'Appliances'          ),
        ( 'pe', 'Pests'               ),
        ( 'ex', 'Exterior'            ),
        ( 'in', 'Interior'            ),
        ( 'sa', 'Safety'              ),
        ( 'ot', 'Others'              ),
    )

    creator   = models.ForeignKey( User, related_name = 'creator' )
    approver  = models.ForeignKey( User, related_name = 'approver' )
    comments  = models.TextField( blank = True )
    status    = models.CharField( max_length = 1, choices = STATUS_CHOICES, default = 'p' )
    quote     = models.DecimalField( max_digits = 8, decimal_places = 2, null = True, blank = True )
    payment   = models.DecimalField( max_digits = 8, decimal_places = 2, null = True, blank = True )
    work_type = models.CharField( max_length = 2, choices = WORK_TYPE_CHOICES )
    vendor    = models.ForeignKey( Vendor, null = True, blank = True )
    created   = models.DateTimeField( auto_now_add = True )
    modified  = models.DateTimeField( auto_now = True )

class OrderCreateForm( ModelForm ) :
    class Meta :
        model = Order
        fields = (
            'creator',
            'approver',
            'comments',
            'work_type',
        )

So far I am using the generic view CreateView for a Order model in urls.py

url(
    r'^orders/create/$',
    CreateView.as_view(
        model = Order,
        template_name = 'doors/orders/create.html',
        form_class = OrderCreateForm
    ),
    name = 'orders_create'
),

In my template, I simply have

{% extends "base.html" %}

{% block title %}Create order{% endblock %}

{% block content %}
    {{ form }}
{% endblock %}

and it create a partial form (with no submit button) like this

However, I want to control the visibility of the creator and approver fields depending on user.get_profile().user_type. If the logged in user’s user_type isn’t a manager, then the creator is automatically set as user and approver will be set also set automatically. If the user_type is a manager, then that user can specify both fields to different users.

I also want to be able to name the actual labels of each fields. For example, change Work type to Category.

Currently I’m thinking the only way to do this is to do {% for field in form %} inside the template and run a bunch of lines like {% if field.name == "creator" %}. Is there an easier way than a bunch of controlled loops and if-then statements?

  • 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-31T23:41:25+00:00Added an answer on May 31, 2026 at 11:41 pm

    I want to control the visibility of the creator and approver fields depending on user.get_profile().user_type. If the logged in user’s user_type isn’t a manager, then the creator is automatically set as user and approver will be set also set automatically. If the user_type is a manager, then that user can specify both fields to different users.

    I’d make another form for just user:

    class UserOrderCreateForm( ModelForm ) :
        class Meta :
            model = Order
            fields = (
                'comments',
                'work_type',
            )
    
        def __init__(self, *args, **kwargs):
             self.user = kwargs.pop('user')
            super(UserOrderCreateForm, self).__init__(*args, **kwargs)
    
        def save(self):
            result = super(UserOrderCreateForm, self).save(commit=False)
            result.creator = self.user
            result.approver = self.user.some_other_field # "will be set also set automatically"
            result.save()
            return result
    

    Then you can decide what form to use in view. Use it like

    form = UserOrderCreateForm(user=request.user)
    

    I think you will have to spend some time to make it work with CreateView. That’s why I don’t like CBV. 🙂

    I also want to be able to name the actual labels of each fields. For example, change Work type to Category.

    work_type = models.CharField( max_length = 2, choices = WORK_TYPE_CHOICES, verbose_name=u'Category')
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a text area in my form which accepts all possible characters from
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
For some reason, after submitting a string like this Jack’s Spindle from a text
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
I am currently running into a problem where an element is coming back from
Does anyone know how can I replace this 2 symbol below from the string

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.