I have an app called profile which has de model Profile(models.Model). Then, I have another app called offer with the model Offer(models.Model) and it has almost all the attributes of the Profile one, so I wanted to include a form with the attributes of profile in the offer.
In the forms.py in the offer app, I have created:
from django.forms import ModelForm
from profile.models import Profile
from offer.models import Offer
class ProfileOfferForm(ModelForm):
#extrafields
class Meta:
model = Profile
exclude = ('min_salary',)
And then in the admin.py (in the app Offer too) I have:
from django.contrib import admin
from django import forms
from offer.models import *
from offer.forms import *
# Register your models here.
from profile.models import Profile
#admin.register(Offer)
#admin.site.register(Offer, OfferAdmin, ProfileForm)
class OfferAdmin(admin.ModelAdmin):
inlines = [
QuestionInline,
AutomatismInline,
CandidateInline
]
form = ProfileOfferForm
And I get no errors, the only problem is that de fields of Profile don't appear in the administration in the section Offer. Am Ileaving something?
Any help will be appreciated,
Thanks!
It looks like you forget to register your custom Admin to your Model.
# At the bottom do something like this
admin.site.register(Offer, OfferAdmin)
Related
I'm trying to display every actions performed on my models on an admin page, using LogEntry. So far I see actions done through the admin (which is the way LogEntry works by default if I understood well), but I would like to catch actions performed through my VueJS frontend as well.
Not sure how to do this properly !
This is my code in admin.py (django 4.0)
from django.contrib import admin
from .models import *
from django.contrib.admin.models import LogEntry, ADDITION, CHANGE
class MoniterLog(admin.ModelAdmin):
list_display = ('action_time','user','content_type','object_repr','change_message','action_flag')
list_filter = ['action_time','user','content_type']
ordering = ('-action_time',)
admin.site.register(LogEntry,MoniterLog)
I want to create a custom user model with some extra fields, among which a contact list of other users. I would like to extend AbstractUser instead of creating a new model with a one-to-one link to User.
from django.contrib.auth import get_user_model
from django.contrib.auth.models import AbstractUser
from django.db import models
class CustomUser(AbstractUser):
a_custom_field = models.IntegerField(default=0)
# per-user contact list
contacts = models.ManyToManyField(get_user_model())
This code doesn't work. Throwing this error during makemigrations.
django.core.exceptions.ImproperlyConfigured: AUTH_USER_MODEL refers to model 'users.CustomUser' that has not been installed
The error totally makes sense, but what's the right way to achieve this?
I found the solution just by digging more into the django docs.
The problem is that I cannot use get_user_model() before the user model has been created.
The solution is using the class name as a string. So, this code works great:
from django.contrib.auth import get_user_model
from django.contrib.auth.models import AbstractUser
from django.db import models
class CustomUser(AbstractUser):
a_custom_field = models.IntegerField(default=0)
# per-user contact list
contacts = models.ManyToManyField('CustomUser')
Has anyone seen this before? I've tried making new apps, projects, etc.
All thats in my admin.py file is:
from django.contrib import admin
from . models import UserProfile, Tribe, Membership
# Register your models here.
admin.site.register(Tribe)
admin.site.register(Membership)
admin.site.register(UserProfile)
I've not got any static files or css in the app..?
Create a class that inherit admin.ModelAdmin, update the fields to be shown in the list_display tuple, and register TribeAdmin instead of Tribe. Do the same for the rest.
from django.contrib import admin
from . models import UserProfile, Tribe, Membership
# Register your models here.
class TribeAdmin(admin.ModelAdmin):
list_display = ('field_1', 'field_2',)
admin.site.register(Tribe, TribeAdmin)
# admin.site.register(Membership)
# admin.site.register(UserProfile)
For all the available options, have a look at the documentation or an easy to understand beginner tutorial from the DjangoBook (please note its for an outdated Django Version, but fields works with Django 1.8)
With Django 1.8 you can use.
#admin.register(Tribe)
class TribeAdmin(admin.ModelAdmin):
list_display = ('field',)
I followed the django import-export manual but the Import Export buttons do not appear in my admin screen.
This is what I have in my admin.py. Is there anything else I need to do? I have added import-export to my settings.py.
from django.contrib import admin
from costtool import models as m
from costtool.models import UserProfile, Prices
from import_export import resources
from import_export.admin import ImportExportModelAdmin, ImportMixin
class PriceResource(resources.ModelResource):
class Meta:
model = Prices
class PriceAdmin(ImportExportModelAdmin):
resource_class = PriceResource
pass
admin.site.register(UserProfile)
admin.site.register(Prices)
Just tell the admin what ModelAdmin to use:
admin.site.register(Prices, PriceAdmin)
You can check Django's ModelAdmin doc and try to use the new register decorator if you're using Django 1.7
I'm working in a form with a m2m field. I want that this field looks like the horizontal interface of the django admin site... ¿how i can do it?
thanks...
You need to use the FilteredSelectMultiple widget
from django.contrib.admin.widgets import FilteredSelectMultiple
from django import forms
from .models import Person
class PersonForm(forms.ModelForm):
some_field = forms.ModelMultipleChoiceField(Person.objects.all(), widget=FilteredSelectMultiple("Person", False, attrs={'rows':'2'}))
class Meta:
model = Person
You will also need to include the Javascript and CSS used in the admin. Here's an example