I'm trying to add a field to 'Groups' within the Django admin - for instance, when you create a group in the backend, you define 'Name' and 'Permissions', and I'd like to add a field to that list (CharField). Does this require a new app, or can I extend the Group model in my root models.py?
New to django and python here, so sorry if the question is badly worded.
Here's what I have so far:
#models.py
from django.db import models
from django.contrib.auth.models import Group
class AppDBName(Group):
AppDB = models.CharField(max_length=30)
def __unicode__(self):
return self.AppDB
#admin.py
from django.contrib import admin
from .models import AppDBName
admin.site.register(AppDBName)
You can try to create a new model, which will extend the Django built-in group model. To do this you should link a new model, say GroupExtend, to the original one with a OneToOne field, like it can be done for user (link):
from django.contrib.auth.models import Group
class GroupExtend(models.Model):
group = models.OneToOneField(Group, on_delete=models.CASCADE)
# other fields
You don't need to create new app just override the django admin models.py in to your project and customize it as per your requirement.
https://docs.djangoproject.com/en/1.8/topics/db/models/
Related
I'm building a django app and the django admin page doesn't seem to be reflecting the changes I make to admin.py. For example if I want to exclude some fields, or customizing the admin change list, nothing changes in the actual page. The only thing that seems to be reflected properly is the fact that I can register the models, and they will show up.
Here's my admin.py
from django.contrib import admin
from .models import Form, Biuletyn, Ogloszenie, Album
class FormAdmin(admin.ModelAdmin):
exclude = ('img',)
class BiuletynAdmin(admin.ModelAdmin):
list_display = ('name', 'date')
class OgloszenieAdmin(admin.ModelAdmin):
fields = ('name', 'date', 'upload')
admin.site.register(Form)
admin.site.register(Biuletyn)
admin.site.register(Ogloszenie)
admin.site.register(Album)
P.S. Please ignore the weird model names. The site is actually in a different language :D
This is how you register your ModelAdmin:
admin.site.register(Form, FormAdmin)
admin.site.register(Biuletyn, BiuletynAdmin)
admin.site.register(Ogloszenie, OgloszenieAdmin)
This how to register your Models:
admin.site.register(Album)
For more details you may refer to Django official documentation at: https://docs.djangoproject.com/en/3.2/ref/contrib/admin/#modeladmin-objects
Add admin_class
So for example for the Form model:
admin.site.register(Form, admin_class=FormAdmin)
I am creating a new User model for my Django project. I have been many people calling their custom user model, XXXUser and custom user manager, XXXUserManager.
I was wondering if there is a reason for this. Can you just create a custom user and still call it User? Does this create conflicts in the code?
Basically you can. But for readability purposes it's better to do it XxxUser, if you see XxxUser you are instantly understand that this is custom one. And you need to keep in mind that you should replace some code that is common for base usage.
Such as(should be replaces)
from django.contrib.auth.models import User
Should be
from django.contrib.auth import get_user_model
User = get_user_model()
And if you reference to User model in your models.py you need to
from django.conf import settings
class SomeModel(models.Model):
field = models.RetationField(settings.AUTH_USER_MODEL)
Also do not forget to set your settings.AUTH_USER_MODEL that references to your custom one
If you want custom user models then take a look at subclassing either AbstractUser or AbstractBaseUser detailed in the documentation here https://docs.djangoproject.com/en/1.10/topics/auth/customizing/#extending-django-s-default-user
You can then do something like the following:
from django.contrib.auth.models import AbstractUser
from django.db import models
from django.utils.translation import ugettext_lazy as _
class KarmaUser(AbstractUser):
karma = models.PositiveIntegerField(verbose_name=_("karma"), default=0, blank=True)
# Inside project/settings.py
AUTH_USER_MODEL = "profiles.KarmaUser"
You could also create your own completely seperate model and tie it back to the user with a one-to-one relationship
The documentation shows
We have a number of fields and we want to let our users filter based on the price or the release_date. We create a FilterSet for this:
import django_filters
class ProductFilter(django_filters.FilterSet):
class Meta:
model = Product
fields = ['price', 'release_date']
Where does this code get placed to create a FilterSet? Is it in models or views? Thanks.
Wherever you want, I mean models.py, views.py or even a new file called filters.py. Because you will use that filter in views.py, so you can import the filters from everywhere in your project.
For me, I think a file filters.py in the app is the best place.
So in your views.py import the filters like this:
from .filters import ProductFilter
I have three models - two of them are in one app, and the third one is on the another. The structure is like this:
taapp.models:
class Teachers(model.Model):
fullname = models.CharField(max_length=50)
...
class TeachersScale(model.Model):
teacher = models.ForeignKey("Teachers")
abbr = models.ForeignKey("questions.QuestionTypes")
questions.models:
class QuestionTypes(models.Model):
abbr = models.CharField(max_length=5)
......
I registered all these models to admin:
taapp.admin:
from taapp.models import Teachers
from taapp.models import TeachersScale
from django.contrib import admin
from admin_forms import TeachersAdmin, TeachersScaleAdmin
admin.site.register(Teachers, TeachersAdmin)
admin.site.register(TeachersScale, TeachersScaleAdmin)
taapp.admin_forms:
from django import forms
from django.contrib import admin
class TeachersAdmin(admin.ModelAdmin):
list_display = ('fullname', 'email', 'registration_date')
class TeachersScaleAdmin(admin.ModelAdmin):
list_display = ('teacher', 'abbr')
list_filter = ['teacher','abbr']
When I try to add a field to TeachersScale in admin site, I get the following error:
DatabaseError at /admin/taapp/teachersscale/add/
(1146, "Table 'taapp.questions_questiontypes' doesn't exist")
It treats QuestionTypes, as it is a model in taapp. How to solve it? Or is there something wrong with my db design?
I tried TabularInline for QuestionTypes to see if reverse adding works. Well, it works:
questions.admin:
class TeachersScaleInline(admin.TabularInline):
model = TeachersScale
class QuestionTypesAdmin(admin.ModelAdmin):
inlines = [TeachersScaleInline]
Thanks in advance.
It looks like you haven't actually created your questions table, or if you have you've forced it into a different database. Foreign keys expect to share the same database, and it's perfectly standard to have multiple apps sharing the same database. That's why the app name is part of the automatically generated table name.
In Django admin I want to override and implement my own form for a model (e.g. Invoice model).
I want the invoice form to have auto-fill fields for customer name, product name and I also want to do custom validation (such as credit limit for a customer). How can I override the default form provided by Django admin and implement my own?
I am new to Django, I appreciate any pointers.
You can override forms for django's built-in admin by setting form attribute of ModelAdmin to your own form class. See:
https://docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contrib.admin.ModelAdmin.form
https://docs.djangoproject.com/en/dev/ref/contrib/admin/#adding-custom-validation-to-the-admin
It's also possible to override form template - have a look at https://docs.djangoproject.com/en/dev/ref/contrib/admin/#custom-template-options
If you're looking specifically for autocomplete I can recommend https://github.com/crucialfelix/django-ajax-selects
How to override a form in the django admin according to the docs:
from django import forms
from django.contrib import admin
from myapp.models import Person
class PersonForm(forms.ModelForm):
class Meta:
model = Person
exclude = ['name']
class PersonAdmin(admin.ModelAdmin):
exclude = ['age']
form = PersonForm