Django unique, null and blank CharField giving ‘already exists’ error on Admin page

None of the answers clearly describe the root of the problem. Normally in the db you can make a field null=True, unique=True and it will work… because NULL != NULL. So each blank value is still considered unique. But unfortunately for CharFields Django will save an empty string “” (because when you submit a form … Read more

Django AdminForm field default value

Assuming the value is based on ‘request’ you should use this: class MyModelAdmin(admin.ModelAdmin): def get_form(self, request, obj=None, **kwargs): form = super(MyModelAdmin, self).get_form(request, obj, **kwargs) form.base_fields[‘my_field_name’].initial=”abcd” return form

How to filter choices in Django2’s autocomplete_fields?

If you are using autocomplete_fields for a ManyToManyField on ‘self’, this example will exclude the current object. Get the current object’s id by overriding get_form: field_for_autocomplete = None def get_form(self, request, obj=None, **kwargs): if obj: self.field_for_autocomplete = obj.pk return super(MyAdmin, self).get_form(request, obj, **kwargs) Next, override get_search_results. Modify the queryset only for your model’s autocomplete URI: … Read more

How to limit fields in django-admin depending on user?

I think there is a more easy way to do that: Guest we have the same problem of Blog-Post blog/models.py: Class Blog(models.Model): … #fields like autor, title, stuff.. … class Post(models.Model): … #fields like blog, title, stuff.. … approved = models.BooleanField(default=False) approved_by = models.ForeignKey(User) class Meta: permissions = ( (“can_approve_post”, “Can approve post”), ) And … Read more

Django’s ModelForm – where is the list of Meta options?

Had this question myself today. For completeness, here is the documentation that currently exists: https://docs.djangoproject.com/en/dev/topics/forms/modelforms/#modelforms-overriding-default-fields And an excerpt from django/forms/models.py: class ModelFormOptions: def __init__(self, options=None): self.model = getattr(options, ‘model’, None) self.fields = getattr(options, ‘fields’, None) self.exclude = getattr(options, ‘exclude’, None) self.widgets = getattr(options, ‘widgets’, None) self.localized_fields = getattr(options, ‘localized_fields’, None) self.labels = getattr(options, ‘labels’, None) … Read more

Only showing year in django admin, a YearField instead of DateField?

I found this solution which solves the whole thing quite elegantly I think (not my code): import datetime YEAR_CHOICES = [] for r in range(1980, (datetime.datetime.now().year+1)): YEAR_CHOICES.append((r,r)) year = models.IntegerField(_(‘year’), choices=YEAR_CHOICES, default=datetime.datetime.now().year) Edit the range start to extend the list 🙂

How can I MODIFY django to create “view” permission?

This is how I changed Django 1.0.2 to add ‘view’ permissions. Sorry there is no diff available. [X] 1. Added ‘view’ to default permission list #./contrib/auth/management/__init__.py def _get_all_permissions(opts): “Returns (codename, name) for all permissions in the given opts.” perms = [] for action in (‘add’, ‘change’, ‘delete’, ‘view’): perms.append((_get_permission_codename(action, opts), u’Can %s %s’ % (action, … Read more

For a django model, how can I get the django admin URL to add another, or list objects, etc.?

http://docs.djangoproject.com/en/dev/ref/contrib/admin/#reversing-admin-urls obj = coconut_transportation.swallow.objects.all()[34] # list url url = reverse(“admin:coconut_transportation_swallow_changelist”) # change url url = reverse(“admin:coconut_transportation_swallow_change”, args=[obj.id]) # add url url = reverse(“admin:coconut_transportation_swallow_add”)

tech