Django ModelChoiceField: filtering query set and setting default value as an object

Override the init method and accept a new keyword argument class AccountDetailsForm(forms.Form): … adminuser = forms.ModelChoiceField(queryset=User.objects.all()) def __init__(self, *args, **kwargs): accountid = kwargs.pop(‘accountid’, None) super(AccountDetailsForm, self).__init__(*args, **kwargs) if accountid: self.fields[‘adminuser’].queryset = User.objects.filter(account=accountid) form = AccountDetailsForm(accountid=3) You can always just set the choices manually in the view as well. form = AccountDetailsForm() form.fields[‘adminuser’].queryset = User.objects.filter(account=accountid) Be … Read more

Overriding the save method in Django ModelForm

In your save you have to have the argument commit. If anything overrides your form, or wants to modify what it’s saving, it will do save(commit=False), modify the output, and then save it itself. Also, your ModelForm should return the model it’s saving. Usually a ModelForm’s save will look something like: def save(self, commit=True): m … Read more

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

You might actually look into the internationally standardized format E.164, recommended by Twilio for example (who have a service and an API for sending SMS or phone-calls via REST requests). This is likely to be the most universal way to store phone numbers, in particular if you have international numbers work with. Phone by PhoneNumberField … Read more

How can I restrict Django’s GenericForeignKey to a list of models?

For example, your apps are app and app2 and there are A, B models in app and there are C, D models in app2. you want to see only app.A and app.B and app2.C from django.db import models class TaggedItem(models.Model): tag = models.SlugField() limit = models.Q(app_label=”app”, model=”a”) | models.Q(app_label=”app”, model=”b”) | models.Q(app_label=”app2″, model=”c”) content_type = … Read more

Django-Registration & Django-Profile, using your own custom form

You’re halfway there – you’ve successfully built a custom form that replaces the default form. But you’re attempting to do your custom processing with a save() method on your model form. That was possible in older versions of django-registration, but I can see from the fact that you specified a backend in your URL conf … Read more

Applying bootstrap styles to django forms

If you prefer not to use 3rd party tools then essentially you need to add attributes to your classes, I prefer to do this by having a base class that my model forms inherit from class BootstrapModelForm(ModelForm): def __init__(self, *args, **kwargs): super(BootstrapModelForm, self).__init__(*args, **kwargs) for field in iter(self.fields): self.fields[field].widget.attrs.update({ ‘class’: ‘form-control’ }) Can easily be … Read more

Django form field choices, adding an attribute

You’d have to subclass the field to take whatever means of specifying the title you’d like and the widget to display the new attribute. If you had something like this (note: entirely untested): from django import forms from django.utils.html import escape from django.utils.encoding import force_unicode class SelectWithTitles(forms.Select): def __init__(self, *args, **kwargs): super(SelectWithTitles, self).__init__(*args, **kwargs) # … Read more