I've used django-admin and assigned a django-form to it. I need to fill up one of my custom rows with a random dynamic data. I can't use initial from form attributes BTW. I need to get the data from my URL and my URL is generated with another component.
Field values can be passed into a form by appending them to the form’s URL using the following structure:
baseURL?variable1=value
Replace baseURL with the form’s URL, variable1 with the variable associated with the field to be filled in and value with the appropriate field value. To fill in multiple fields, insert the ampersand symbol (&) before any additional variables:
baseURL?variable1=value&variable2=value
Related
Using a ListView Class-based-view, I am looping over the objects present in the database of a certain model in my HTML template, and, for instance, I can access an object's "body_text" attribute with the following syntax: {{object.body_text}}
What if I wanted to only show the first 20 characters of that "body_text" attribute in my HTML template?
How can I set that?
1st Method
Use the truncatechars filter in your HTML template.Truncates a string if it is longer than the specified number of characters. Truncated strings will end with a translatable ellipsis character (“…”).
{{object.body_text|truncatechars:20}}
Reference:
https://docs.djangoproject.com/en/dev/ref/templates/builtins/?from=olddocs#truncatechars
2nd Method
Use the slice filter in your HTML template.
{{object.body_text|slice:":20"}}
Referernce: https://docs.djangoproject.com/en/dev/ref/templates/builtins/#slice
Found it, eventually.
Use the |slice:":" filter in your HTML template.
For example, if you only want to display the first 10 characters of a given attribute, use:
{{object.body_text|slice:":10"}}
I need a Django Form Field which will take a list of strings.
I'll iterate over this list and create a new model object for each string.
I can't do a Model Multiple Choice Field, because the model objects aren't created until after form submission, and I can't do a Multiple Choice Field, because I need to accept arbitrary strings, not just a series of pre-defined options.
Anyone know how to do this?
Just use a regular text field delimited by commas. After you handle the form submission in the view do a comma string split based on that field. Then iterate over each one creating and saving a new model. Shouldn't be too hard.
In my case to process list of strings I used forms.JSONField(decoder="array") in my forms.py in form class
I came up with a solution -- a little hacky but it works for now.
After grabbing the form data, I stash the list in a variable:
event_locations = form_data.get('event_locations', None)
Then I remove it from form_data, so the Django Form never gets the list:
if event_locations:
del form_data['event_locations']
I instantiate my form with form_data, and handle the list separately:
f = NewEventForm(form_data)
...
for loc in event_locations:
#create new models here
I realize this doesn't directly solve the question I asked, because we still don't have a Django Form Field taking a list, but it's a way to pass in a list to a view that takes a form and be able to handle it.
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 normally use something like this "Tag.object.annotate(num_post=Count('post')).filter(num_post__gt=2)" to get tags with more than 2 posts. I want to get number of posts with a field value (e.g post.published=True) and annote over them so that I get tags with number of published posts bigger than some value. How would I do that?
Edit:
What I want is not filter over annotated objects. What I want is something like this: Tag.objects.annotate(num_post=Count("posts that have published field set to true!")). What I am trying to learn is, how to put post that have published field set to true in Count function.
You can just replace the 2 in ..._gt=2 with some other variable - for example, a variable that gets passed into the view, or a request.GET value, or similar.
Is that what you're trying to do?
I'd like a URL of the form:
... field1/eq/value1/field2/gt/value2/ ...
Where I want to filter the contents of the page in the view function based on an arbitrary number of fields (the names of which are not known in advance).
I've tried:
(r'^((?P<field>\w+)/(?P<op>[a-z]+)/(?P<value>\w+)/)*$', my_view)
But the keyword arguments are filled with the last set of three field/op/value to occur in the URL.
Is there any way that I could populate a list or dictionary based on a variable number of URl fields like this?
Or should I be doing this some completely different way?
Don't do this.
Use the query string. ?field,eq,value&field,gt,value will work out much better.