What’s the best way to store a phone number in Django models?

I am storing a phone number in model like this: phone_number = models.CharField(max_length=12) The user would enter a phone number and I would use the phone number for SMS authentication. This application would be used globally. So I would also need a country code. Is CharField a good way to store a phone number? And, … Read more

Define css class in django Forms

Assume I have a form class SampleClass(forms.Form): name = forms.CharField(max_length=30) age = forms.IntegerField() django_hacker = forms.BooleanField(required=False) Is there a way for me to define css classes on each field such that I can then use jQuery based on class in my rendered page? I was hoping not to have to manually build the form. 14 … Read more

Create empty queryset by default in django form fields

I have this fields in form: city = forms.ModelChoiceField(label=”city”, queryset=MyCity.objects.all()) district = forms.ModelChoiceField(label=”district”, queryset=MyDistrict.objects.all()) area = forms.ModelChoiceField(label=”area”, queryset=MyArea.objects.all()) district comes from click on city and area comes from click on area. With queryset=MyDistrict.objects.all() and queryset=MyArea.objects.all() form will be very heavy. How can I make querysets empty by default? 2 Answers 2

How do I add a placeholder on a CharField in Django?

Take this very simple form for example: class SearchForm(Form): q = forms.CharField(label=”search”) This gets rendered in the template: <input type=”text” name=”q” id=”id_q” /> However, I want to add the placeholder attribute to this field with a value of Search so that the HTML would look something like: <input type=”text” name=”q” id=”id_q” placeholder=”Search” /> Preferably I … Read more

How do I filter ForeignKey choices in a Django ModelForm?

Say I have the following in my models.py: class Company(models.Model): name = … class Rate(models.Model): company = models.ForeignKey(Company) name = … class Client(models.Model): name = … company = models.ForeignKey(Company) base_rate = models.ForeignKey(Rate) I.e. there are multiple Companies, each having a range of Rates and Clients. Each Client should have a base Rate that is chosen … Read more

Django set default form values

I have a Model as follows: class TankJournal(models.Model): user = models.ForeignKey(User) tank = models.ForeignKey(TankProfile) ts = models.IntegerField(max_length=15) title = models.CharField(max_length=50) body = models.TextField() I also have a model form for the above model as follows: class JournalForm(ModelForm): tank = forms.IntegerField(widget=forms.HiddenInput()) class Meta: model = TankJournal exclude = (‘user’,’ts’) I want to know how to set … Read more