How to override the default value of a Model Field from an Abstract Base Class

0 votes

I've got some code that looks like this:

class BaseMessage(models.Model):
    is_public = models.BooleanField(default=False)
    # some more fields...

    class Meta:
        abstract = True

class Message(BaseMessage):
    # some fields...

and I'd like to override the default value of the is_public field in the Message model so that it's True for that model.

I've looked through some relevant Django docs and poked around the model objects but I'm having trouble finding the right place to do this. Any suggestions?

Jun 30, 2020 in Python by kartik
• 37,520 points
2,919 views

1 answer to this question.

0 votes

Hello @kartik,

You can do this as follows:

class BaseMessage(models.Model):
    is_public = models.BooleanField(default=False)
    # some more fields...

    class Meta:
        abstract = True

class Message(BaseMessage):
    # some fields...
Message._meta.get_field('is_public').default = True

It works, because the field on Message is a different instance than the field on BaseMessage. However,It depends a lot on how django internals currently work.

Hope it helps!!

Thank You!!

answered Jun 30, 2020 by Niroj
• 82,840 points

Related Questions In Python

0 votes
3 answers

How to get the return value from a thread using python?

FWIW, the multiprocessing module has a nice interface for ...READ MORE

answered Dec 15, 2020 in Python by Roshni
• 10,480 points
107,695 views
0 votes
1 answer

I have a dictonary in python how to access the value field?

dic={"car":["limo","sedan"]} print (dic.keys())    <-----------------------Fetch the key "car" print (dic['car'][0])   <------------------------Fetch ...READ MORE

answered Dec 20, 2018 in Python by Shuvodip
786 views
+1 vote
1 answer

How to find the index of a particular value in a dataframe?

First, use the dataframe to match the ...READ MORE

answered Apr 8, 2019 in Python by Esha
281,520 views
0 votes
1 answer

How to find the value of a row in a csv file in python?

If you want to find the value ...READ MORE

answered May 20, 2019 in Python by Sanam
19,208 views
0 votes
1 answer

How to temporarily disable a foreign key constraint in MySQL?

Hello @kartik, To turn off foreign key constraint ...READ MORE

answered Jun 23, 2020 in Python by Niroj
• 82,840 points
2,437 views
0 votes
1 answer

How do I use Django templates without the rest of Django?

Hello @kartik, Let's say you have this important ...READ MORE

answered Jun 23, 2020 in Python by Niroj
• 82,840 points
1,555 views
0 votes
1 answer

How do I add a link from the Django admin page of one object to the admin page of a related object?

Hello @kartik, Set show_change_link to True (False by default) in your inline ...READ MORE

answered Jun 12, 2020 in Python by Niroj
• 82,840 points
10,589 views
0 votes
1 answer

How to make the user in a model default to the current user?

Hello @kartik, You have to override get_changeform_initial_data method in your ...READ MORE

answered Jun 12, 2020 in Python by Niroj
• 82,840 points
4,056 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP