Django custom forms Dynamic Raw Id - python

I have a table with a foreign key. My problem is that ther's a lot of registers, so I need do that:
But all I've found was for the Admin Panel. Any idea for a custom form without admin?

Raw id, isn't needed any more, since there's autocomplete in the admin. This is also the solution for a model in the frontend. However, you should not bake it yourself, but just use Django Autocomplete Light. An excerpt from the tutorial:
Autocompletes are based on 3 moving parts:
widget compatible with the model field, does the initial rendering,
javascript widget initialization code, to trigger the autocomplete,
and a view used by the widget script to get results from.

Related

Edit FileField file contents from Django Admin

I have a situation where I have python scripts that are stored as files that need to be modified from the Admin panel. Currently some of my models can have scripts attached to them:
class Action(PolymorphicModel):
title = models.CharField(max_length=100)
text = BBCodeField('text', blank=True)
action_script = models.FileField(upload_to='action_scripts')
In my Admin panel I can upload a file but I have been asked to make this file editable in-place if the user has the right permissions. I've searched all over for an idiomatic way to do this, and so far I've come up with the following ideas:
Override the FileField widget to include an text edit box and save button. This seems very brute forced
Replace models.FileField with subclass of models.TextField, implement read from file, save to file, but then I lose the upload widget and data still gets stored in DB. This seems dirty because in the database all I'm storing is a filename. FileField feels like the right field type here.
Write a custom ModelForm that includes forms.TextField. Handle save to file using the save method. I'm not sure how to accomplish this because I would have to generate a filename on the fly and I don't have a primary key yet. Maybe some sort of UUID?
I'm leaning against door number 3 right now, but I've only been using Django for a week, so I'm not sure this is the right way to go, or even how to do it. Also I would need to have a custom ModelForm for every model that inherits from Action, which violates DRY.
Further complications:
Not all Actions need scripts. If the textbox is empty, the FileField can just become Null.
Action is subclassed into a handful of derived classes. The editing functionality should extend to each model's Admin panel in a scalable way, like the other fields.
This question seems generic enough that its solution would be useful for those wishing to edit HTML or CSS from the admin, etc, the same as WordPress does. Searching along those lines unfortunately brings up results to do with skinning the admin, which I'm not after. In my situation I'm trying to avoid storing python code in the database for security reasons. The Admin interface is not going to be exposed to the end users so in theory it should be safe, and I get the benefit of caching, compilation and locality (less hits to the DB).
Many thanks.

Django Admin UI add and edit objects in table form

I have a Django app that displays tables on the user end of database queries. I would like the admin to be able to edit those models and objects in table form. Ex: click a value and type in the new value instead of having to click the object and hit save. I’m unsure if there is an easy/conventional way to do this. Thanks!
I like use django-crispy-forms. Flexible solution for use django forms. Easy integrate ajax.

django admin - depending on radio button choice, show different fields

I am trying to do this cool stuff in django admin
I have a model which has a ImageField. What I want is that the my other coworkers are able to decide if they want to upload image or just paste the image link.
like
<radio>: upload <radio>: link
and depending on choice, respective field (which is model's imageField) will show up for under these radio buttons.
how can I achieve this in django admin?
You'll need to add the additional field to your admin form class, then add some JavaScript to show or hide the appropriate field on page load, and also add an event handler to show the field to upload or paste in the link.
Both fields would need to allow blank=True and then you'd need to add a clean() method to make sure one of the fields is populated and then set the value appropriately. You might be better off using two separate fields.
You can leverage the Media inner class to easily add the JavaScript to the page without having to alter the change_form.html template for that app. Check out: https://docs.djangoproject.com/en/1.7/topics/forms/media/ for examples of how to add custom CSS and JavaScript to forms.

Create new foreign key in form

I'm using django autocomplete_light and have two models connected via one-to-many relationship. Model A has a ForeignKey field TAG to model B. It all works, but I can only select the existing Tag, it is not possible to automatically add new Tag, even though it is possible to freely type in the box.
How can I "intercept" validation and create the suitable database entry for tag in time?
You could use an add another popup like in django admin.
Here's a live example using this code. The design is not very very good but it demonstrates the point.

Django, userfriendly way to order objects in filter_horizontal widget in admin

I often use the filter_horizontal attribute for many-to-many relationships which renders a FilteredSelectMultiple widget.
The downside of this widget arises when the user needs to be able to order the objects.
This is not user friendly because the order can only be modified on the change_view of the object itself. Which may live in a complete different area of the admin.
Does anyone have ideas about how this can be improved? Is there a FilterSelectMultipleOrderable widget out there? (I didn't find it yet)
Update!!
We made a version which extends the default django widget.
https://github.com/fabrique/django-sortedm2m
This seems to do the job:
http://pypi.python.org/pypi?:action=display&name=django-sortedm2m
It just renders a list so it could use some styling.

Categories