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

  • Home
  • SEARCH
  • 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 8536343
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T10:38:27+00:00 2026-06-11T10:38:27+00:00

I noticed that, for the most part, Django does not enforce default values on

  • 0

I noticed that, for the most part, Django does not enforce default values on the database level.

Field definition in model:

description = models.TextField(default='')

SQL:

description | text   | not null

If I add a row using raw SQL (as described here: https://docs.djangoproject.com/en/dev/topics/db/sql/#executing-custom-sql-directly), and don’t include a value for a field that has a default, will the default still be used?

  • 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-11T10:38:28+00:00Added an answer on June 11, 2026 at 10:38 am

    No.

    At least, not based on a test I just did.

    models.py:

    class Recipient(models.Model):
        mailing = models.ForeignKey(Mailing, related_name="recipients")
        email = models.EmailField()
        how_sent = models.CharField(max_length=1, choices=SENT_TYPES, default="U")
        user = models.ForeignKey(User, blank=True, null=True)
        date_sent = models.DateTimeField(auto_now_add=True)
        date_viewed = models.DateTimeField(null=True, blank=True)
    
        def __unicode__(self):
            return "%s -> %s (%s)" % (self.mailing, self.email, self.date_sent)
    

    And then I rand this code:

    >>> cursor.execute("""insert into mailings_recipient (mailing_id, email) values (3, 'test@example.org');""")
    Traceback (most recent call last):
      File "<console>", line 1, in <module>
      File "path/to/django/db/backends/util.py", line 40, in execute
        return self.cursor.execute(sql, params)
      File "/path/to/django-pyodbc/sql_server/pyodbc/base.py", line 326, in execute
        return self.cursor.execute(sql, params)
    IntegrityError: ('23000', "[23000] [FreeTDS][SQL Server]Cannot insert the value NULL into column 'how_sent', table 'database.dbo.mailings_recipient'; column does not allow nulls. INSERT fails. (515) (SQLExecDirectW)")
    

    If Django entered default values for you, it would have sent a value of “U” for how_sent but instead I got an error.

    Then if we create a record using Django,

    Recipient.objects.create(mailing=Mailing.objects.all()[0], email='test@example.org') 
    

    this is the corresponding SQL:

    SET NOCOUNT ON INSERT INTO [mailings_recipient] ([mailing_id], [email], [how_sent], [user_id], [date_sent], [date_viewed]) VALUES (1, test@example.org, U, None, 2012-09-14 14:48:59, None) 
    ;SELECT SCOPE_IDENTITY()
    

    So if you are really asking whether default values will be entered for you if you use something like cursor.execute("INSERT INTO...") then the answer is no, it will not set up those default values for you. As you pointed out, Django does not set up the default values for you when creating your database schema, it merely sets up those default values when performing a normal save.

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

Sidebar

Related Questions

I noticed that most scripts for e-mail validation do not use DNS information to
I've noticed that on most, if not all, standard web controls in the System.Web.UI.WebControls
I have noticed that most Objective-C coders never use the self->ivar syntax when accessing
I've noticed that most 3d gaming/rendering environments represent solids as a mesh of (usually
I am studying how the android.speech package works and I noticed that most of
I'm currently building my own login script and have noticed that most append the
Looking through a few SQL implementations, I noticed that most DBMSs support defining an
I have never come across this issue but most recently I noticed that a
Like most, I recently downloaded the latest version of XCode (4.3.1). I've noticed that
I've been using a few javascript libraries, and I noticed that most of them

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.