Manage slug for django - python

I try to use django-uuslug, to manage unique and unicode slug with django. This project seems interesting, it is better to use an existing project to reinvent the wheel. However I have a question, I would to know if it's be possible to specify one column more, to make the slugify on the current object and the optionnal column. For example if we have a site column and want have slug unique per site not per table.
uuslug(self.title, instance=self, unique_per_column=self.site)
Otherwise is there a better way to manage slug in django or not.

If you have a look at the source code of uuslug, it is possible to do what you want like this:
uuslug(self.title, instance=self, filter_dict={'site': self.site})
this will cause uuslug to filter the queryset of instance's model to rows with the same value of site field before doing the uniqueness check on that queryset

Related

Implement items ordering in Django

Suppose, I want to build a simple TODO-app. I want to make it possible to create todo-items, and also it should be possible to rearrange items manually.
I made following model:
class Item(models.Model):
title = models.CharField(max_length=500)
Now, I need to add a special field, let's call it order, to keep custom ordering. It should be unique, and it should be greater for any new record, so I tried to make an AutoField.
class Item(models.Model):
title = models.CharField(max_length=500)
order = models.AutoField(primary_key=False)
But it turned out that Django doesn't support several auto fields for a single model.
I guess, it should be possible to write custom raw SQL code and use Postgres sequences directly, but it would look really ugly, and I don't want to write DB-specific code for such simple functionality.
So, here is a question: what is a common way to implement items ordering in Django? It sounds like a very common requirement, and, I think, it should be a simple way to do this.
As it turns out there is no straightforward way to implement this in Django. There are packages which help you, like this one
But I would recommend just look at their model implementation and fit your needs. models.py
You could use Item.objects.count() to automatically increment your field. Plug it in the save() method of your model so that your field is calculated each time you create an instance.

Group list items by field value in the Django admin

I'm trying to group the items in the Django admin app by a specific field (e.g. date).
So I've added to the queryset in admin.ModelAdmin.getQueryset() the following:
queryset = queryset.values('date').annotate(Sum('amount'))
But this doesn't work because in this case, a dict is returned instead of a queryset.
I started exploring what's inside the django/contrib/admin folder, and I think something need to be done before sending the object to the template change_list.html.
I'm not sure but I think the class in views/main.py (admin folder) might need some change.
Can anybody confirm that what I'm trying to do is achievable at all?
Please find below a representation of what I'm trying to achieve:
Follow the below example in URL. it's has great way to understand with override django admin with custom queryset and groupby data
https://medium.com/#hakibenita/how-to-turn-django-admin-into-a-lightweight-dashboard-a0e0bbf609ad
I found this useful: https://github.com/xacce/django_admin_grouper
You can simply define the group in ClassAdmin
class RecordAdmin(admin.ModelAdmin):
def regroup_by(self):
return 'category'
The repo overrides Django's change_list_results.html. If your RecordAdmin has method reggroup_by than it inserts a row with the name of the category. If reggroup_by is missing it works as usual.

Populating value in a newly added field to models from a different table

Its Django 1.7, I have User model with date_joined field. I have another related model Userprofile. Also, the database is already existing with few thousand entries already.
Now, i want to add this date_joined field to userprofile
date_joined = models.DateTimeField(auto_now_add=True, null=True)
And for the already existing rows in the Userprofile model, i want to put the value already there in the User model.
Although i can write a simple function to do this, I was curious if there is a simpler way to do that through models directly during creating that column only.
As you already know, date_joined is already present in your data - Django provides it by default. It sounds like you want to duplicate that existing data into your UserProfile instances. Duplicating data is never a good idea. It can go out of sync, you need to pepper your code with synchronization functions, etc. My advice is to NOT try to do what you're trying to do. Just utilize the existing data as needed. Django is giving you a "gimme" here and it sounds like you want to make things more complicated than they need to be. Remove date_joined from UserProfile.

What is the best approach to design model fields that is like a list in Django?

Let's say I have a model called 'SysApp'. Each system has 5 documents. Each document has fields:
Title
URL to the file (external url)
Description
Rather than defining multiple fields like
title_1,
url_1,
description_1,
title_2,
url_2,
description_2
(Hardcoded approach)
is there a better way to handle this type of use case?
One way of doing is to create a model storing each document and then SysApp will reference each document using a ForeignKey. However I still have to create field like document_1, document_2 etc. Also it would be quite difficult for editors to manage when there are 100+ SysApp and 3-400+ documents.
Is it possible to manage these fields like a list or dictionary?
Thank you
I think the best way to organize your 'SysApp - documents' relationship, assuming that each document is related to only one sysapp, is to use ForeignKey, as you mentioned.
In that case you'll only have to create 2 models: the first one is SysApp with a name field and the second is Document with fields title, url to file, description and a foreignkey to SysApp. Now you can create documents and attach them to the sys you want. So you do not need to specify document_2, document_3 etc. fields.
If you need to attach one document to more than one sysapp use ManyToMany instead of ForeignKey.

How to improve performance in visualizing data from database on admin site of django?

This are my first steps with django so please take no offense if this seems trivial.
I have a very huge table mynames (~ 1 000 000 entrys) and I want to make this table editable in the django admin site. I hooked things up like described in the official django book: I have a model, and I registered it by admin.site.register(mymodel). I can see the "table" on my admin site and I can click on it to see the first page full of names. That is nice so far. As soon as I click on the "show next page button" at the bottom of the page, the query seams to take forever.
Where could the problem be?
Update:
I added an index to the relevant column and now it is fast. I thought by doing
name = models.CharField(max_length=100, db_index=True, unique=True)
in the model definition there would be an index for this column. But there was none. Only unique index. Is this the way it should be or do I miss something?
It would be easier to see what the problem is if you pasted your full model class. Is there a specified ordering on the table? If so, you need to create a corresponding index to make pagination on that field fast. Is the list_display attribute set on the ModelAdmin class for the model? By default, django should order the rows based on the primary key so you should not need an additional index. Also check if you have overridden the __unicode__ method for the model in a way that causes additional database lookups when showing the entity in django admins table?

Categories

Resources