How to Specify a mySQL ENUM in a Django model

0 votes
How do I go about specifying and using an ENUM in a Django model?
Jun 24, 2020 in Python by kartik
• 37,510 points
1,214 views

1 answer to this question.

0 votes

Hello @kartik,

You can follow this snippet below:

MAYBECHOICE = (
    ('y', 'Yes'),
    ('n', 'No'),
    ('u', 'Unknown'),
)

And you define a charfield in your model :

married = models.CharField(max_length=1, choices=MAYBECHOICE)

You can do the same with integer fields if you don't like to have letters in your db.

In that case, rewrite your choices:

MAYBECHOICE = (
    (0, 'Yes'),
    (1, 'No'),
    (2, 'Unknown'),
)

Thank You!

answered Jun 24, 2020 by Niroj
• 82,880 points

Related Questions In Python

0 votes
2 answers

How do I connect to a MySQL Database in Python?

connect mysql database with python import MySQLdb db = ...READ MORE

answered Mar 28, 2019 in Python by rajesh
• 1,270 points
1,592 views
0 votes
1 answer

How to get all related Django model objects in Python?

This actually gives you the property names ...READ MORE

answered Nov 14, 2018 in Python by Nymeria
• 3,560 points

edited Dec 18, 2018 by Nymeria 6,306 views
0 votes
0 answers

How to calculate accuracy in a logistic regression model in python?

What accuracy score is considered a good ...READ MORE

Jul 30, 2019 in Python by Waseem
• 4,540 points
1,610 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,880 points
3,668 views
0 votes
1 answer

How to run Django's test database only in memory?

Hello @kartik, If you set your database engine ...READ MORE

answered Jun 24, 2020 in Python by Niroj
• 82,880 points
3,893 views
0 votes
1 answer
0 votes
1 answer

ERROR: mysql 1018 (HY000): Can't read dir of '.' (errno: 13)

Hello @kartik, You need to set ownership and ...READ MORE

answered Jun 24, 2020 in Python by Niroj
• 82,880 points
2,155 views
0 votes
1 answer

Error:No module named MySQLdb

Hii @kartik, Try this code: pip install PyMySQL and then ...READ MORE

answered Jun 26, 2020 in Python by Niroj
• 82,880 points
14,685 views
0 votes
1 answer

How to load a custom JS file in Django admin home?

Hello @kartik, You can override templates/admin/index.html and add the JavaScript ...READ MORE

answered May 14, 2020 in Python by Niroj
• 82,880 points
7,297 views
0 votes
1 answer

How to check if an element is present in a Django queryset?

Hello @kartik, You can use the following code: if ...READ MORE

answered May 27, 2020 in Python by Niroj
• 82,880 points
14,205 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