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

The Archive Base Latest Questions

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

In my django app, I have these models class Lesson(models.Model): name = models.CharField(max_length=200) course

  • 0

In my django app, I have these models

class Lesson(models.Model):
    name = models.CharField(max_length=200)
    course = models.ForeignKey(Course)
    ...
class Course(models.Model):
    title = models.CharField(max_length=200)
    ...

I use two forms to get/create a Course instance in my Edit_Lesson template.In one choice_form I use a ModelChoiceField to create a dropdown list in the template.In the other course_name_form I provide a textfield by using CharField where the user can enter a course name.

I need to do the validation for these.If initially there are no courses ie Course.objects.all() is an empty list,the drop down list in choice_form will not have any items for the user to select from.Then,if user forgets to enter a name in the textfield of course_name_form,there wont be any course instance selected/created.When this happens I would like to show the user an error message asking him to input a course name.

I think ,I won’t be able to write the validation code in the forms.Should I create a error_list=[] in my view and put the error in it and display it?This means ,the validation happens in view.I am not sure if this is the right way

please advise

  • 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-11T18:06:34+00:00Added an answer on June 11, 2026 at 6:06 pm

    You can override the clean() method of your model forms to perform validation that requires knowledge of a broader scope.

    In your case, you might do

    def clean(self):
       if not Course.objects.count() and not self.cleaned_data['course_name']:
           raise ValidationError(u"Some sensible message")
       return self.cleaned_data
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have these Django models: class Group(models.Model): name = models.CharField(max_length=100) parent_group = models.ManyToManyField("self", blank=True)
I have 3 models in my django app class Company(models.Model): name = models.CharField(_(Name), max_length
in a django-tastypie app I have the following Django-models: class Car(models.Model): name=models.CharField('name',max_length=64) class CarTrack(models.Model):
I have these 3 models in models.py class Customer(models.Model): name = models.CharField(max_length=50) .... class
I have a model: class Server(models.Model): serverId = models.IntegerField(verbose_name=_(serverId)) name = models.CharField(max_length=200, verbose_name=_(server_name)) ip
In Django, I have an app with models like this: class Artist(models.Model): name =
Let's say I've defined this model: class Identifier(models.Model): user = models.ForeignKey(User) key = models.CharField(max_length=64)
I have these models in my app models.py: class A(models.Model): #some details pass class
I have this model: class Category(models.Model): name = models.CharField() description = models.CharField(blank=True) parent =
I've got a basic Item model in my Django app: class Item(models.Model): 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.