I am trying to show a single instance (row db) from a model where multiple instances use the same field (column) value for multiple rows. To clarify this statement, I have the following situation:
ID/Title/Slug/Modified 1 Car A 1s ago 2 Car A 2s ago 3 House B 1s ago
If the above small table was my db, I want my Django admin page to display different rows based on my slug field (column) showing the last edited version (I have another column for the time ... So the above the table will show on the administration page as follows:
ID/Title/Slug/Modified 1 Car A 1s ago 3 House B 1s ago
Although lines 1 and 2 have different pk, they have the same slime, I want only one of them with a later time ...
I can achieve this in my views.py as follows
existing_data = MyModel.objects.filter(slug=slug).latest('modified')
but this is because I am looking for a specific instance of a bullet. If I were not, I could also use group_by ...
I am trying to get this display on the admin page. I tried using the following methods in the model manager,
class ModelManager(models.Manager): def get_query_set(self): return super(ModelManager, self).get_query_set().group_by('title')
but i get this error
'QuerySet' object has no attribute 'group_by'
Then I read , and they implemented raw sql in the model manager, which I tried to copy into my situation properly,
class ModelManager(models.Manager): def uniques(self): cursor = connection.cursor() cursor.execute(""" SELECT * FROM eventform_event GROUP BY slug """) return [row[0] for row in cursor.fetchone()]
Im my model I have
objects = ModelManager()
I'm just not sure how to get the admin model to view my custom manager, which does not override get_query_set. When I used this user manager to override get_query_set, I get this error
'long' object has no attribute '__getitem__'
I also tried to test the values ββ(), values_list (), distinct (), etc., but they all give me errors ... distinct () tells me that my database (mysql) does not support this function. Now I am sure that I need to switch databases to achieve this function, and now I have no idea to experiment with ... Does anyone know how to achieve this functionality .. Thanks.
#
On my admin.py page, I can get a side filter (list_filter) to show unique entries based on the slug column in this thread of recommendation ...
Unfortunately, I cannot get the rows displayed on the admin page so that my model is unique based on a specific column ...