I would like to use a ready-made select list of all the world countries (I don't want to store it in database) to fill an input field of a Django modelform, but I don't know which way to go.
Is there a way to do so?
http://code.google.com/p/django-countries/
Enjoy!
The only way I know to do that is to hardcode it into the template.
This list has it done already:
http://snippets.dzone.com/posts/show/376
by following this guide you can have automatic countries populated in your form field
http://code.google.com/p/django-countries/source/browse/trunk/docs/overview.txt
Related
I need to write a Django custom field where the stored value is not fully displayed.
For example if the stored value is "079684" I need it to be displayed as "---84". Is it possible to do so ? If yes how should I implement my custom field ?
I have solved this by overriding from_db_value() of the custom field method.
You could store your value as a string in your model and than use slice in template to show only last two character.
{{ value|slice:"-2:" }}
I have a model with some HStoreField attributes and I can't seem to use Django's ORM HStoreField to query those values using LIKE.
When doing Model.objects.filter(hstoreattr__values__contains=['text']), the queryset only contains rows in which hstoreattr has any value that matches text exactly.
What I'm looking for is a way to search by, say, te instead of text and those same rows be returned as well. I'm aware this is possible in a raw PostgreSQL query but I'm looking for a solution that uses Django ORM.
If you want to check value of particular key in every object if it contains 'te', you can do:
Model.objects.filter(hstoreattr__your_key__icontains='te')
If you want to check if any key in your hstore field contains 'te', you will need to create your own lookup in django, because by default django won't do such thing. Refer to custom lookups in django docs for more info.
As far as I can remember, you cannot filter in values. If you want to filter in values, you have to pass a column and value you are referencing to. When you want it to be case insensitive use __icontains.
Although you cannot filter by all values, you can filter by all keys. Just like you showed in your code.
If you want to search for 'text' in all objects in key named let's say 'fo' - just do smth like this:
Model.objects.filter(hstoreattr__icontains={'fo': 'text'})
I'm trying to use the HTML5 month input field for my form and want to store as a models.DateField. My question is this possible? If it is how do I do it? If not why and should I just be using models.CharField?
`
I would just save it as a CharField. To my knowledge, Django doesn't have a way to only save Months, but you can easily simply save the number and work around that
Suppose I have a model Order, which has a column num -- an order number. Now I want to filter several rows from this model in admin view. Having 1 value, I do:
http://bla-bla-bla/admin/app/order/?num__exact=11534
How can I do this when I have several values?
Or should I use queryset()? How then I should send a list of values to request?
in should work, try this in the url
http://bla-bla-bla/admin/app/order/?num__in=11534,11535,11536
Don't forget that whatever you put in the query string has to be allowed for the admin interface. You can't put in filters that weren't defined there - ever since this security release https://www.djangoproject.com/weblog/2010/dec/22/security/
I have a model in my django project called "change" and it has a field called "change_type". There are multiple values within the change_type field to include "move", "new", "edit" and others with new types being added randomly over any given period of time. I am currently using normal django queries to select groups of entries within the change model.
Is there a quick method to determine what unique entries are in the change_type field? Is there a quick method to return a count of each entry type?
After finding the solution, it is really simple.
Change.objects.all().values('change_type').distinct()
Putting it together:
occurrences = {}
change_types = Change.objects.values_list('change_type', flat=True).distinct()
for type in change_types:
occurrences[type] = Change.objects.filter(change_type=type).count()
http://docs.djangoproject.com/en/dev/topics/db/managers/ maybe that will be helpful for You.
This can now be much more effectively implemented using aggregation:
Change.objects.values('change_type').annotate(Count('change_type'))
The output contain change_type__count field for each respective change_type.