I'm using django and I have model names Tags.
It is used in 15 different models accros my entire application as ManyToManyField.
I need to get a len / count of all occurances of specific tag.
How am I suposed to do that ? Do I have to use Aggregate / annotate ? Select related ? Or something completelty different. Ofcourse using forloops is not an opiton.
If you want be able to query in one request how many items are related to a Tag if they are different Django model, you should use the contenttypes framework provided by Django and not define Tags as a ManyToMany field.
Samples in the documentation is exactly what you want.
https://docs.djangoproject.com/en/1.8/ref/contrib/contenttypes/#generic-relations
https://docs.djangoproject.com/en/1.8/ref/contrib/contenttypes/#reverse-generic-relations
Related
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.
Say that I have a many to many relationship field in my model, what I'm trying to do is get all the related entities if the field in the entity is equivalent to MAMMAL. I'm currently doing this in a list comprehension but wondering if there's a more elegant solution that django models provide.
[related_entity for related_entity in related_entity.related_entities.all() if
related_entity.entity_type.entity_type_label == 'MAMMAL']
Based on Django documentation(https://docs.djangoproject.com/en/1.9/topics/db/examples/many_to_many/), you could use:
entity.objects.filter(related_entity__entity_type__entity_type_label == 'MAMMAL')
A many-to-many field gives you a manager, just like the standard Model.objects() one, and you can filter it in exactly the same way:
related_entity.filter(entity_type__entity_type_label="MAMMAL")
in my Django model I have a m2m relation with an intermediate model that contain the two foreign keys of the respective two models.
My problems is that, the first model may have many entries and when I associate it with other model I have a too large drop down widget in the admin.
The default ManyToManyField widget (with filter_horizontal set) may be ok, but it is compatible only with m2m relations....
Is there a solution (even 3rd parts) for the ForeignKeyFields?
Any suggestions?
Edit:
For the other users, I've solved my problem using django-selectable
http://django-selectable.readthedocs.org/en/v0.8.X/index.html
The 3rd party component Django Ajax Selects does exactly what you are looking for.
I played around with django-selectable as well, but found django-autocomplete-light (https://github.com/yourlabs/django-autocomplete-light‎) easier to use and more flexible for what I wanted to achieve.
Perhaps it can be useful to you as well.
I have a model called "occurrence" which tracks occurrences of species at different sites. The model has 4 fields.
refID (foreign key to the reference source of data)
siteID (foreign key to site)
speciesID (foreign key to species)
abundance (integer)
I know I could create a model-form to add an entry. But modelforms would be tedious because, most of the time I want to enter data for dozens or hundreds of species with the same combination of siteID and refID. I have created my own data entry form in the template to select a refID and siteID, and use jQuery to add new lines for speciesID and abundance. Thus, I have a single refID + siteID combination, with many speciesIDs + abundance lines. Then, the idea is to iterate over all the added lines and save all the occurrences in the view.
The problem is that validation of this form is quite difficult, as I have to do everything "manually" in the view. This seems like it might be a common problem, so I wonder.....
Am I missing a pre-existing Django solution here?
Probably you're, as far as I understand your question.
have a look at Formsets
The simple solution to this problem turned out to be using a modelform to save a single instance at a time. I made the refID and siteID fields "sticky" by passing back the saved values for those fields to the reloaded form using the "initial" argument to the modelform. This way, I can use all the built in form validation.
I'm trying to do such a thing: I have two models, connected via Foreign Key, and every instance of first model needs to be linked with exactly 4 instances of second (I use InlineAdmin to create them at the same time using extra=4 and max_num=4).
There are two problems:
1 - I need each of four models to has it's own default read only value for one of it's fields, this value needs to be selected from the field's choices option. But I need them to be read only (I know about readonly_fields, but it's useless for that. Javascript don't seems to be a good solution...
2 - I need to specify default values for some field for all four models at the same time editing only one field. I'm thinking of two possible solutions: javascript or one additional, "fifth" model with all hidden fields except the one I need so I can override save() to use it's values for other models and delete it.
But which is the right way?
Try this documentation, it might help:
https://docs.djangoproject.com/en/1.5/ref/models/fields/#editable