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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T04:03:40+00:00 2026-05-26T04:03:40+00:00

15 class Profile(models.Model): 16 17 User profile model 18 19 user = models.ForeignKey(User, unique=True)

  • 0
 15 class Profile(models.Model):
 16     """
 17     User profile model
 18     """
 19     user = models.ForeignKey(User, unique=True)
 20     country = models.CharField('Country', blank=True, null=True, default='',\
 21                                max_length=50, choices=country_list())
 22     is_active = models.BooleanField("Email Activated")

I have a model like above with country set to blank=True, null=True.

However, in the form that is presented to the end user, I required the country field to be completed.

So I redefine the field in the Model Form like this to ‘force’ it to become required:

 77 class ProfileEditPersonalForm(forms.ModelForm):
 78 
 79     class Meta:
 80         model = Profile
 81         fields = ('email',
 82                   'sec_email',  
 83                   'image',
 84                   'first_name',
 85                   'middle_name',
 86                   'last_name',
 87                   'country',
 88                   'number',
 89                   'fax',)
 90 
 98     country =  forms.ChoiceField(label='Country', choices = country_list())

So the country field is just an example (there are tons of them). Is there a better more DRY way of doing this?

  • 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-26T04:03:40+00:00Added an answer on May 26, 2026 at 4:03 am

    You can modify the fields in __init__ in the form. This is DRY since the label, queryset and everything else will be used from the model. This can also be useful for overriding other things (e.g. limiting querysets/choices, adding a help text, changing a label, …).

    class ProfileEditPersonalForm(forms.ModelForm):    
        def __init__(self, *args, **kwargs):
            super().__init__(*args, **kwargs)
            self.fields['country'].required = True
    
        class Meta:
            model = Profile
            fields = (...)
    

    Here is a blog post that describes the same "technique": http://collingrady.wordpress.com/2008/07/24/useful-form-tricks-in-django/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given the Model: class Profile(models.Model): user = models.ForeignKey(User, unique=True) class Thingie(models.Model): children = models.ManyToManyField('self',
Given these two models: class Profile(models.Model): user = models.ForeignKey(User, unique=True, verbose_name=_('user')) about = models.TextField(_('about'),
models.py class Team(models.Model): name = models.CharField(max_length=25, unique=True) def __unicode__(self): return self.name class UserProfile(models.Model): user
I have a profile. class UserProfile(models.Model): user = models.ForeignKey(User, related_name=%(class)s, unique=True) providers = models.ManyToManyField(ServiceProvider,
I have an extended UserProfile model in django: class UserProfile(models.Model): user = models.ForeignKey(User, unique=True)
I have the following models -- class UserProfile(models.Model): user = models.ForeignKey(User, unique=True) ... class
I have a model as follows class UserPrivacy(models.Model): user = models.ForeignKey(User) profile = models.SmallIntegerField(default=1,
I have the following in models.py: class Choices(models.Model): description = models.CharField(max_length=300) def __unicode__(self): return
I have a simple question. This is my profile: class Profile(models.Model): user = models.ForeignKey(User,
I have two models Profile and Avatar. models.py class Profile(models.Model): user = models.ForeignKey(User) profile_name

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.