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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T18:42:13+00:00 2026-06-13T18:42:13+00:00

Suppose I have a class with two fields and both fields are blank, but

  • 0

Suppose I have a class with two fields and both fields are blank, but i want at least one field to be filled.

class MyClass(models.Model):
   url1 = models.URLField(blank=True)
   url2 = models.URLField(blank=True)

   def clean(self):
      if not self.url1 and not self.url2:
      raise forms.ValidationError('message here')
      return self.url1

I think because I set the two fields to blank=True.
Don’t know if using clean() here is true or not and also what to return from it.

Nothing showing in {{form.non_field_errors}}

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-13T18:42:14+00:00Added an answer on June 13, 2026 at 6:42 pm

    you can use the clean() method on a form object. Currently you are trying to make data validation not on a Form object but instead on a database model.

    Have a look on https://docs.djangoproject.com/en/dev/ref/forms/validation/ to know more about form validation.

    from django.db import models
    from django import forms
    
    class MyClass(models.Model):
       "Your model"
       url1 = models.URLField(blank=True)
       url2 = models.URLField(blank=True)
    
    
    class MyClassForm(forms.ModelForm):
        "Your form object"
        def clean(self):
           if not self.cleaned_data['url1'] and not self.cleaned_data['url2']:
               raise forms.ValidationError('message here')
           return self.cleaned_data
    
        class Meta:
            model = MyClass
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose that I have two classes, first a class without any properties, fields or
Suppose we have two classes: class Base { private: int x; public: void f();
suppose you have two (or more) classes with private member vectors: class A {
Suppose I have this class: class MyClass(object): def uiFunc(self, MainWindow): self.attr1 = foo self.attr2
The scenario Suppose I have the following two model classes: public class ProductColor {
Suppose that I have two data types Foo and Bar. Foo has fields x
Suppose I have the following code which creates two radio buttons: <li id=foli517 class=
suppose I have two classes class A extends class B class A has its
Suppose you have two models, User and City, joined by a third model CityPermission:
Suppose I have two types, TypeA and TypeB, that I want to register with

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.