Questions tagged [django-model-field]
112 questions
143
votes
6 answers
What is the best django model field to use to represent a US dollar amount?
I need to store a U.S. $ dollar amount in a field of a Django model. What is the best model field type to use? I need to be able to have the user enter this value (with error checking, only want a number accurate to cents), format it for output to…

MikeN
- 45,039
- 49
- 151
- 227
5
votes
1 answer
Python Django How to Create Hash Fields inside django models.py
I'm working on a Python(3.6) & Django(1.10) project in which I need to save some user credentials of the third party services like username, password, and email, I'm implementing only rest API, so there's no form.py at all.
So, How can I make hash…

Abdul Rehman
- 5,326
- 9
- 77
- 150
4
votes
1 answer
How to index columns created by django-modeltranslation to Elastic, using django-elasticsearch-dsl?
I've translated my Model fields with django-modeltranslation and implemented search using django-elasticsearch-dsl.
Problem:
django-modeltranslation creates translation fields in DB, and not in my Models, and search is working only for fields…

Oleg Barbos
- 178
- 1
- 10
4
votes
3 answers
How to reduce max_length of CharField in Django when data already exists in db
I have a CharField on a Django (1.9) model:
alias = models.CharField(max_length=50)
The app is being used and already has data, and there are objects using all 50 characters already. What is the simplest method to reduce the max_length of this…

43Tesseracts
- 4,617
- 8
- 48
- 94
3
votes
2 answers
Django: Model attribute looks for field works in view, but not in template
Say I have a model:
from django.db import models
class Foo(models.Model)
fav_color = models.CharField(max_length=250, help_text="What's your fav color?")
print(Foo.fav_color.field.help_text) # prints: What's your fav color?
Say I have a view…

run_the_race
- 1,344
- 2
- 36
- 62
3
votes
1 answer
Using if condition with django choice field in templates
I want to conditionally display fields of a form in django template. I use a current value of a choice field as follows. However I can not see this working as expected and nothing is rendered at all in the
tag. In the model: PENDING =…

Indika Rajapaksha
- 1,056
- 1
- 14
- 36
3
votes
1 answer
Overriding methods for defining custom model field in django
I have been trying to define custom django model field in python. I referred the django docs at following location https://docs.djangoproject.com/en/1.10/howto/custom-model-fields/. However, I am confused over the following methods(which I have…

Mangu Singh Rajpurohit
- 10,806
- 4
- 68
- 97
3
votes
3 answers
Empty label in widget Select
Here in Forms we can use the empty_label. I use widget Select, my choices are from database, does it possible to use here empty_label? In model I have:
position = forms.IntegerField(label=position_label, required=False,
…

Leon
- 6,316
- 19
- 62
- 97
2
votes
1 answer
Unique together between field and model connected by ForeignKey django
I have two models.
class Post(models.Model):
title = models.CharField(max_length=150)
class Attachment(models.Model):
type = models.CharField(choices=AttachmentChoices.get_choices(), max_length=15)
link = models.URLField()
post =…

user15125625
- 23
- 4
2
votes
0 answers
VSCode autocomplete for Django orm lookups/property not working
How to autocomplete django orm property or lookups in VSCode?
Just like it works in Pycharm Professional Edition as follow:

Ali Bi
- 76
- 5
2
votes
1 answer
Django PositiveBigIntegerField
So, im trying to work on my apps and I have PositiveBigIntegerField's in some of my models. I thought for sure it was already included with Django but now i'm starting to think otherwise. Whenever I run my server, im getting an error stating that…

Alec
- 27
- 8
2
votes
1 answer
Are there advantages to using IntegerChoices field in a Django model if it's not shown to users?
I'm using IntegerChoices in my Django (3.2) model.
class AType(db.IntegerChoices):
UNKNOWN = 0, 'Unknown'
SOMETHING = 1, 'Something'
ANOTHER_THING = 2, 'Another thing'
A_THIRD_THING = 3, 'A third thing'
class…

dfrankow
- 20,191
- 41
- 152
- 214
2
votes
1 answer
Flagging a django model instance during save for later processing
I'm running into a problem with using a flag to mark model instances for future processing. I have a class:
class MyModel(models.Model):
processed = models.BooleanField(default=False)
changed = models.DateTimeField(auto_now=True)
# More…

Steven Spasbo
- 647
- 7
- 18
2
votes
1 answer
django form populate multiple identical field form in one submit
I don't want to use django form class as they will not give me much flexibility.
I have a form where will random number field in easy request. i am trying to populate the multiple value of forms that appears.
this is my models.py
class…
user12551055
2
votes
1 answer
Django model choice Text to Integer
I'm trying to create a django model using Text as a value for the IntegerField
class User(models.Model):
class UserRole(models.IntegerChoices):
FULLACCESS = 0, _('full_access_user')
READONLY = 1, _('read_only_user')
…

Pythonizer
- 1,080
- 4
- 15
- 25