i have a basic django-jython aplication which i am using admin panel on it, and it seems even if i declare the 'list_per_page = 25' in my admin.py for each modeladmin class, it cant paginate the results. i have a table that contains 900.000 rows and django is trying to put all the rows in a one page. when i declare the 'list_per_page' option, it puts page numbers and total count of data below the page but then puts all the rows in every page. i know it sounds strange but i cant find any solution. Here is my Model class also here is my ModelAdmin code sample :
class MahalleAdmin ( admin.ModelAdmin ):
list_display = ('KOD','AD','TIP','YETKILIIDAREKODU','KOYKODU')
list_filter = ['AD','TIP','YETKILIIDAREKODU','KOYKODU']
search_fields = ['KOD','AD','TIP','YETKILIIDAREKODU','KOYKODU']
paginator = paginator.Paginator
list_per_page = 25
class MAHALLE_MERSIN ( models.Model ):
class Meta:
db_table = 'MAHALLE_MERSIN'
verbose_name = 'MERSİN MAHALLELERİ'.decode('Latin5')
verbose_name_plural = 'MERSİN MAHALLELERİ'.decode('Latin5')
#app_label = 'MESKİ ERP'.decode('Latin5')
def __unicode__(self):
return self.AD
KOD = models.AutoField(primary_key = True)
AD = models.CharField( max_length=512)
TANITIMKODU = models.IntegerField()
TIP = models.ForeignKey(MAHALLE_TIP ,db_column= 'TIP')
YETKILIIDAREKODU = models.ForeignKey( KURUM , db_column='YETKILIIDAREKODU')
KOYKODU = models.ForeignKey(KOY_MERSIN ,db_column= 'KOYKODU')
My guess is that you are running with the Django-Jython Oracle driver (doj.backends.zxjdbc.oracle), am I right? There is an issue with pagination in Oracle that I logged with the developer. It seems to be a problem only with the Oracle implementation of doj, the MySQL driver works fine.
Oracle Pagination in Admin interface
I really need to get this working for a project, so I'm gonna try to fix it myself and submit a patch. I will update you if/when it is fixed.
Related
I want to make a music library app and in the admin/artist page I would like to link to a list with all the albums by the artist.
Models look like this:
class Artist(models.Model):
artistName = models.CharField(max_length=100, blank = False)
genre = models.CharField(max_length=100, blank = False)
def __str__(self):
return self.artistName
class Album(models.Model):
artist = models.ForeignKey(Artist, on_delete=models.CASCADE)
albumName = models.CharField(max_length=500, blank = False)
year = models.IntegerField(blank = False)
def __str__(self):
return self.albumName
Now for the admin part, I imagine that I make an ArtistAdmin model and then somehow use list_display to link to a filtered version of the Album model. Can anyone tell me how to do this or suggest a better way of doing it?
I'm having exactly the same need; here's the simplest way I came up so far.
Admin lists have list_filter, with which you can, for example, do that (assuming you register the admin classes and so on):
class AlbumAdmin(admin.ModelAdmin):
list_display = ('artist', 'albumName', 'year',)
list_filter = ('artist',)
This will add a right column in your admin site with the list of artists, and if you click on one, the Albums list will be filtered to show only those which are made by this artist.
Having that, and here's the trick, turns out that even if you don't declare "list_filter", Django will still filter things according to the URL parameters.
So, I activated the filters, I clicked in one of my "artists", so the list got filtered and I could see how it formats the URL, which is something like
[…]/admin/app_name/model_name/?foreignKeyName__id__exact=XX
In your case I imagine it will look similar to:
[…]/admin/music_library/album/?artist__id__exact=XX
Try to follow these steps, then remove the line "list_filter", and you will see that the URL still works.
So now we know that the only thing we need is to pass the param artist__id__exact=XX, so we have to append a column with a button for that.
class ArtistAdmin(admin.ModelAdmin):
list_display = ('artistName', 'genre', 'artists_list_field')
def artists_list_field(self, obj):
base_url = reverse('admin:music_library_album_changelist')
return mark_safe(u'Albums' % (
base_url, obj.id))
With this you have it working.
I hope it's useful!
You can use list_display and list_filter in admin.py like this:
class PersonAdmin(admin.ModelAdmin):
list_display = ('first_name', 'last_name',)
list_filter = ('company__name', 'first_name',) # company is a model which has relation to the Person model
Then in your admin.py you should add PersonAdmin to your model :
admin.site.register(Person, PersonAdmin)
Reference
This might be a simple Django question, but I hope I can get som advice. The following code in admin.py
class ExarbeteMomentInline(admin.TabularInline):
model = ExarbeteMoment
extra=0
class ExarbeteStudent(admin.TabularInline):
model = ExarbeteStudent
extra=0
class ExamensarbeteAdmin(admin.ModelAdmin):
list_display = ('title', 'start_date', 'end_date', 'company')
inlines = [ExarbeteMomentInline, ExarbeteStudent]
admin.site.register(Examensarbete,ExamensarbeteAdmin)
produces what I want, in the admin panel.
But I want a version of it outside the admin panel, so that regular users can enter data. How can I modify the code in a minimal way to get essentially the same page outside the admin region?
Thanks, in advance.
/ADDED/
My models are:
class Examensarbete(models.Model):
title = models.CharField(max_length=10000,default='',blank=True)
start_date = models.DateField('startdatum')
end_date = models.DateField('slutdatum',blank=True,default='',null=True)
company = models.CharField(max_length=200,default='',blank=True)
comment = models.TextField(max_length=1000,default='',blank=True)
kurskod = models.CharField(max_length=100,default='FMAM05')
year = models.CharField(max_length=5,blank=True,default='',null=True)
class ExarbeteMoment(models.Model):
exarbete = models.ForeignKey(Examensarbete,on_delete=models.CASCADE)
typ = models.CharField(max_length=100)
person = models.ForeignKey(Personal,on_delete=models.SET_NULL,null=True)
tim = models.FloatField(null=True)
class ExarbeteStudent(models.Model):
exarbete = models.ForeignKey(Examensarbete,on_delete=models.CASCADE)
first_name=models.CharField(max_length=100)
last_name=models.CharField(max_length=100)
pnr = models.CharField(max_length=13)
program = models.CharField(max_length=5)
kull = models.CharField(max_length=5)
where I have deleted str and Meta. I guess I should be able to solve the problem with the help below, but I can't still figure out how I get what I get in the admin panel, with Examensarbete above and then two subforms with ExarbeteMoment and ExarbeteStudent. And the 'add another item' feature.
Unfortunately I am new to Django, and find it particularly hard to work with forms and formsets. I am not quite sure why, because they should simplify things considerably.
I agree with the comment from Roman Kovalevsky - you will need to write the functions by yourself. Django however supports you with forms and formsets to do this job. As you did not post your models in the question i can only show you an example with some random named variables:
class ExarbeteStudent(forms.Form):
description = forms.CharField(widget=AdminTextareaWidget)
date1 = forms.DateField(widget=AdminDateWidget)
date2 = forms.DateField(widget=AdminDateWidget)
task_list = Task.objects.all()
then you create a formset for the inline models as following
ExarbeteStudentFormSet = forms.formset_factory(ExarbeteStudent,
extra=1,
max_num=60,
can_delete=True,
Here you find some more informations about forms and formsets
https://docs.djangoproject.com/en/2.0/topics/forms/formsets/
I have a self-referencing model in Django 1.5 as shown below:
RELATIONSHIP_PARENT = 1
RELATIONSHIP_BLOCKED = 2
RELATIONSHIP_STATUSES = (
(RELATIONSHIP_PARENT, 'Parent'),
(RELATIONSHIP_BLOCKED, 'Blocked'),
)
class Message(models.Model):
content = models.CharField("Content", max_length=160, db_index=True)
relationships = models.ManyToManyField('self',
through='Relationship',
symmetrical=False,
related_name='related_to')
class Relationship(models.Model):
parent_message = models.ForeignKey(Message, related_name='parent_messages')
child_message = models.ForeignKey(Message, related_name='child_messages')
status = models.IntegerField(choices=RELATIONSHIP_STATUSES)
And I configured Django admin to show me Relationships as inline when viewing individual Message panel as below:
from django.contrib import admin
from demo.models import Message, Relationship
class RelationshipInline(admin.TabularInline):
model = Relationship
extra = 0
fk_name = 'parent_message'
class MessageAdmin(admin.ModelAdmin):
inlines = (RelationshipInline,)
admin.site.register(Message, MessageAdmin)
admin.site.register(Relationship)
I intend to store many messages (with a lot of parent-child connections among them) in the table. Whenever I view individual message via Admin panel, I see something like this:
As shown in the red circle, Django admin collects all messages in the database and display them as drop-down list in the menu. I have read through a few ways to prevent it and the closest that I found is Representing ManyToMany relation in the Admin Panel but when I tried putting raw_id_fields = ('parent_message', ) under RelationshipInline class, it doesn't seem to do anything.
If anyone can recommend me to a link or resource or just show me how to prevent Django from showing every entry/messages in the drop-down list, I would greatly appreciate the help. Thank you.
This should work.
class RelationshipFormSet(BaseInlineFormSet):
def get_queryset(self):
if not hasattr(self, '_queryset'):
criteria = {} #Your criteria here
qs = super(RelationshipFormSet, self).get_queryset().filter(**criteria)
self._queryset = qs
return self._queryset
class RelationshipInline(admin.TabularInline):
model = Relationship
extra = 0
fk_name = 'parent_message'
formset = RelationshipFormSet
The raw_id_fields should go in the admin class:
admin.py
class MessageAdmin(admin.ModelAdmin):
inlines = (RelationshipInline,)
raw_id_fields = ('parent_message', )
I have the following code in my admin.py:
class UserManagedGroupAdmin(admin.ModelAdmin):
inlines = [MembershipInline]
search_fields = ('name', 'leader__username', )
list_display = ('__unicode__', 'leader', )
filter_horizontal = ('permissions', )
raw_id_fields = ('leader', )
admin.site.register(UserManagedGroup, UserManagedGroupAdmin)
The magnifying glass icon for searching doesn't appear in the admin page.
This is what I'm getting:
As you can see it's showing the unicode method of the model instead of the search icon I want.
Field 'leader' is a ForeignKey to User.
Could it be that django disables the search for ForeignKeys to User for security reasons, or am I doing something wrong?
The widget would be perfect for choosing users... I mean, I can't leave a huge select there with every user of my site.
Thanks.
I've found the problem thanks to this message in django-users.
I had to register in the admin the model to which the ForeignKey points to.
The search doesn't work without that.
Hi encounter the same issue but reason's a bit different.
To integrate the User and UserGroup with another app's admin (e.g. some_app)
I added below code to some_app/admin.py
class ProxyUser(User):
class Meta:
proxy = True
verbose_name = User._meta.verbose_name
verbose_name_plural = User._meta.verbose_name_plural
class ProxyGroup(Group):
class Meta:
proxy = True
verbose_name = Group._meta.verbose_name
verbose_name_plural = Group._meta.verbose_name_plural
admin.site.unregister(Group)
admin.site.unregister(User)
admin.site.register(ProxyGroup)
admin.site.register(ProxyUser, UserAdmin)
I think the unregister(...) will affect the other app's admin Globally!
That's another cause of missing search icon.
In the admin.py file add:
admin.site.register(YourModel)
This did the trick, Where YourModel is the model to be displayed with the magnifying glass
I'm working on building a django app that extends the Mezzanine project. Mezzanine has a Gallery app (photos). I'd like to create "portfolio" page that acts as a landing page which has a single image and link to each gallery page.
Each gallery (Gallery) can have multiple images (GalleryImage). I'd like to via the admin select a gallery, then select an image to be displayed. However, I can't seem to figure out what to do.
Here's my model:
class GalleriesThumb(models.Model):
relatedlandingpage = models.ForeignKey(LandingPage)
relatedgallery = models.ForeignKey(Galleries)
thumb = models.ManyToManyField(GalleryImage)
def __unicode__(self):
return self.name
class Galleries(models.Model):
landingpage = models.ForeignKey(LandingPage)
tagline = models.CharField(max_length=100)
galleries = models.ManyToManyField(Gallery)
def __unicode__(self):
return self.name
class LandingPage(models.Model):
gallerytitle = models.CharField(max_length=200)
def __unicode__(self):
return self.name
My admin is something like:
class GalleryInline(admin.InlineModelAdmin)
model = Galleries
model = GalleriesThumb
list_display = galleries
list_display = thumb
class LangingPageAdmin(admin.ModelAdmin):
fieldsets = (
(None, {
'fields': ('gallerytitle')
})
inlines = [GalleryInline,]
I realized that this won't do what i want, but how do I get the list_display on the the images that are related to Galleries. I'm pretty sure it needs to be a method, or am I taking a completing wrong approach if the selections that are made will be defining the content on the page. (I realize that I'm also missing my fields to store the selection in.)
I'm sorry if this a dumb question, but this my first real world attempt an app.
I think this link will resolve your problem
Django 1.2.1 Inline Admin for Many To Many Fields
class GalleryInline(admin.InlineModelAdmin)
model = Galleries.galleries.through