I created poll aplication in Django. Here is admin.py code:
from django.contrib import admin
from poll.models import Question, Choice
class ChoiceInline(admin.StackedInline):
model = Choice
extra = 3
class QuestionAdmin(admin.ModelAdmin):
fieldsets = [
(None, {'fields': ['question_text']}),
('Date information', {'fields': ['pub_date']})
]
inlines = [ChoiceInline]
admin.register(Question, QuestionAdmin)
Also, I added 'poll' to INSTALLED_APPS. Now I have no urls and views for my poll application. Maybe the problem is with it?
Also I have other application 'article', I did the same as with poll, and article has appeared in admin.
You should use admin.site.register instead of just admin.register
Related
Any ideia how to implement form field with plus icon to add ? Like form in django admin.
Thank You
This can be done using ModelAdmin.autocomplete_fields. For example:
# admin.py
from django.contrib import admin
from .models import Car, Driver
#admin.register(Car)
class CarAdmin(admin.ModelAdmin):
autocomplete_fields = [
"driver",
]
#admin.register(Driver)
class DriverAdmin(admin.ModelAdmin):
search_fields = [
"name",
]
I need to add custom fields to Django-registration-redux such as picture and few other fields. Is there a ref or 2 liner guide for how to do it? As per docs I can call RegistrationView in my app views and import my own created model fields to override it. If this is right approach please let me know.
You have to define your custom form and then set your registration url to use the custom form.
Defining your custom form:
users/form.py
from registration.forms import RegistrationForm
from users.models import User
class CustomRegistrationForm(RegistrationForm):
class Meta:
model = User
fields = ('email', 'picture', 'password',)
Your main urls file:
project_name/urls.py
from users.forms import CustomRegistrationForm
urlpatterns = [
[...]
url(r'^registration/register/$', RegistrationView.as_view(
form_class=CustomRegistrationForm), name='registration_register',),
url(r'^registration/', include('registration.backends.default.urls')),
[...]
]
I try to create a view, that will accept POST requests and create new instances of my model(see bottom of post). I follow this tutorial. The problem is that when I access URL associated with view, which inherits from CreateAPIView I dont see a form in html representation of API for creation of new instances and I also see that it accepts GET requests, not POST as it mentioned in documentation.
Page looks like this
My views.py
from django.shortcuts import render
from rest_framework.generics import ListAPIView, CreateAPIView
from datingapp.models import Profile
from .serializers import ProfileSerializer, ProfileCreateSerializer
class ProfilesAPIView(ListAPIView):
queryset = Profile.objects.all()
serializer_class = ProfileSerializer
class ProfileCreateAPIView(CreateAPIView):
queryset = Profile.objects.all()
serializer_class = ProfileCreateSerializer
My urls.py
from django.conf.urls import url
from django.contrib import admin
from datingapp.views import ProfilesAPIView, ProfileCreateAPIView
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'api/profiles/', ProfilesAPIView.as_view(), name='list'),
url(r'api/profiles/create/$', ProfileCreateAPIView.as_view(), name='create')
]
My serializers.py
from rest_framework.serializers import ModelSerializer
from datingapp.models import Profile
class ProfileSerializer(ModelSerializer):
class Meta:
model = Profile
fields = [
'name',
'age',
'heigth'
'location',
]
class ProfileCreateSerializer(ModelSerializer):
class Meta:
model = Profile
fields = [
'name',
'age',
'heigth'
'location',
]
In my settings.py I have crispy_forms installed.
What am I doing wrong ?
UPD: here is what I want to achieve
As you see there is a form and it accepts only POST and also says that GET is not allowed
The problem is in your router. The first pattern matches both api/profiles/ and api/profiles/create/ so the second one will never be evaluated. You are seeing the ProfilesAPIView instead of the create view.
url(r'api/profiles/', ProfilesAPIView.as_view(), name='list'),
url(r'api/profiles/create/$', ProfileCreateAPIView.as_view(), name='create')
To fix it, either swap the order of the urls, or add a $ to the end of the first pattern. r'api/profiles/$'
I was following a tutorial and had a similar problem. Probably I was not following the same version of Django Rest Framework and they had changes.
But I solved this problem doing this.
class AssetBundleList(generics.ListAPIView):
to
class AssetBundleList(generics.ListCreateAPIView):
Hope this helps someone.
Here is my codes:
serializers.py
class UserSerializer(serializers.ModelSerializer):
class Meta:
model = User
fields = ( 'username', 'email')
class AllListingSerializer(serializers.ModelSerializer):
class Meta:
model = Listing
fields = ('name', 'desc', 'thumbnail', 'office_no', 'mobile_no', 'email', 'web ')
views.py
class UserViewSet(generics.ListCreateAPIView):
queryset = User.objects.all()
serializer_class = UserSerializer
class AllListing(generics.ListCreateAPIView):
queryset = Listing.objects.all()
serializer_class = AllListingSerializer
urls.py
urlpatterns = patterns('',
url(r'^$', apiview.UserViewSet),
url(r'^listings/$', apiview.AllListing),
)
But when i goto the base url it shows
init() takes 1 positional argument but 2 were given
and when i goto '/listings/' url, it give me 404 Page Not Found, but I have few listings in the db.
I am pretty new in django. I can't figure out what wrong with them. I am using Django 1.7.1 in virtualwrappr, python 3.4.
You should call .as_view() for each API view:
urlpatterns = patterns('',
url(r'^$', apiview.UserViewSet.as_view()),
url(r'^listings/$', apiview.AllListing.as_view()),
)
Also, consider using REST framework's Routers which provide you with a simple, quick and consistent way of wiring your view logic to a set of URLs.
This happened to me when I extended generics.GenericAPIView in stead of viewsets.GenericViewSet in my custom ViewSet class.
Pretty obvious but easy to miss.
It gives you filter by staff status and superuser status, but what about groups?
Since version 1.3 it can be done using this:
list_filter = ('groups__name')
Of course as #S.Lott explains you must register your customized class in the admin.py file:
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from django.contrib.auth.models import User
class MyUserAdmin(UserAdmin):
list_filter = UserAdmin.list_filter + ('groups__name',)
admin.site.unregister(User)
admin.site.register(User, MyUserAdmin)
See Customizing an Admin form in Django while also using autodiscover
Essentially, you define a customized Admin class with the features you want.
Then unregister and register your revised Admin class.
admin.site.unregister(User)
admin.site.register(User, MyUserAdmin)
Here is a complete example, that inherits from SimpleListFilter, which is available in Django 1.4 and up.
https://docs.djangoproject.com/en/1.4/ref/contrib/admin/#django.contrib.admin.ModelAdmin.list_filter
It support setting all available labels and parameters to create the completely custom filter.
It shows up as "By group" in the filter panel, with a list of all available groups.
from django.contrib.admin import SimpleListFilter
from django.contrib.auth.models import Group
from django.utils.translation import ugettext as _
class GroupListFilter(SimpleListFilter):
title = _('group')
parameter_name = 'group'
def lookups(self, request, model_admin):
items = ()
for group in Group.objects.all():
items += ((str(group.id), str(group.name),),)
return items
def queryset(self, request, queryset):
group_id = request.GET.get(self.parameter_name, None)
if group_id:
return queryset.filter(groups=group_id)
return queryset
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from django.contrib.auth.models import User
class MyUserAdmin(UserAdmin):
list_filter = UserAdmin.list_filter + (GroupListFilter,)
admin.site.unregister(User)
admin.site.register(User, MyUserAdmin)
In later versions of Django, it works exactly as you'd expect:
list_filter = ('groups', )
No need to unregister/register the admin class.