Django: How to take row wise input in Django forms? - python

I want to take row-wise input in Django forms.
The table is as shown in: Image
The rows of 'Source' field should be headings and corresponding to it should be input field belonging to 'Data' field (in html template) such that the input correspondingly goes into proper record.

When I lay out a form using {{form.as_p}}, I get something that looks a bit like this, and which seems to match what you're asking:
If that's not what you're looking for, then you'll need to set your form out manually, as specified in the documents. All of the form attributes - label, name and id are available from your form variable being passed back to the form - it's just a regular HTML form. Lay it out as you want it, and then substitute in the Django variables.
The form fields here correspond to the modelFields in my class Event in the models.py file.

Related

How to work with JSON object after it has been passed from views.py in django?

Consider a django model which stores the name, phone number and address of people.
To allow users to edit its contents, I have created an edit form in html template using bootstrap modals. I want to provide an option to the user to 'leave the field blank to retain old information' (which is not the problem).
Along with this, I would also like to display the current information of each field as present in the database (as a placeholder value of the <input> tag).
In order to do that, I have created a 'context' dictionary in views.py which contains the data to be sent.
My question is, how do I send this dictionary from views.py to an html template without rendering any html page and how to parse this data on the template.
Note: I don't want to render any new page since I am using the bootstrap modal for the edit form.

Create a dynamic admin site

I want to create a dynamic admin site, that based on if the field is blank or not will show that field. So I have a model that has a set number of fields, but for each individual entry will not contain all of the fields in my model and I want to exclude based on if that field is blank.
I have a unique bridge identifier, that correlates to each bridge, and then all of the various different variables that describe the bridge.
I have it set up now that the user will go to a url with the unique bridgekey and then this will create an entry of that bridge. So (as i am testing on my local machine) it would be like localhost/home/brkey and that code in my views.py that corresponds to that url is
However, not every bridge is the same and I have a lot more variables that I would like to include in my model but for now I am just testing on two : prestressed_concrete_deck and reinforced_concrete_coated_bars. What I want is to dynamically create the admin site to not display the prestressed_concrete_deck variable if that field is blank. So instead of displaying all of the variables on the admin site, I want to only display those variables if that bridge has that part, and to not display anything if the field is blank.
Another possible solution to the problem would be to get that unique identifier over to my admins.py. I cant figure out either how to get that individual key over as then I could query in the admins.py. If i knew how to access the bridgekey, I could just query in my admins.py dynamically. So how would I access the brkey for that entry in my admins.py (Something like BridgeModel.brkey ?)
I have tried several different things in my admin.py and have tried the comments suggestion of overwriting the get_fields() method in my admin class, but I am probably syntactically wrong and I am kind of confused what the object it takes exactly is. Is that the actual entry? Or is that the individual field?
Just override the get_fields method in your ModelAdmin class.
You can check the obj is passed as function argument so you can check which fields are empty. The function needs to return a tuple so, you would check if field1 is None and then return (field1, field2, field3) or (field2, field3) depending on the value of field1.
I was using Django 1.6 which did not support overriding the get_fields method. Updated to 1.7 and this method worked perfectly.

Django Form prefix vs. Form set

There exist two ways in Django to process the several forms in one request:
Form prefix
Form sets
In which use case is each one preferable?
In my specific case, the form lists the fields for an object to be updated from a diff. For each field, an action can be defined (like "update value", "keep value"). The page contains forms for several objects.
If you have several different forms classes with same input names, like a PetForm and an OwnerForm with both a name input in the same page, then you have to use a prefix as explained in the documentation you linked.
If you want to have the same form class repeated, ie. to render a table of TicketForm, then you can use Formsets directly: Formsets use the form prefix feature internally and you don't have to worry about it.

How to factorize common fields with deform/colander ?

I need to display two forms depending on the context.
The second form should contain a name and email text input followed by the first form content in that order, but only when the user is not authenticated.
I tried with Python class inheritance but in every tested cases the two new fields were displayed after the content of the first form.
Is there a way to correcly factorise common fields in deform forms or to specify a different display order?
The solution is probably to define the schema imperatively:
http://docs.pylonsproject.org/projects/colander/en/latest/basics.html#defining-a-schema-imperatively

Add Derived Field to Django Admin Change Form

I'd like to add a derived field to a default ModelAdmin.fieldsets like I would by specifying a method and adding it to the ModelAdmin.list_display property, but there doesn't seem to be an easy way to do that (if there is ANY way to do that).
The default Django Admin list view seems to have a lot more options than the change form view does.
Let's say I have two fields for a location: latitude and longitude, and instead of displaying them on a change form I want to display a Google Maps Static Map image instead - I already have a method that will return the src url for the image - I just need a way to add that image to the model change form instead of showing those two fields.
If you write a method and add it to ModelAdmin.readonly_fields, it will appear on the change view.
customize admin view http://docs.djangoproject.com/en/dev/ref/contrib/admin/#ref-contrib-admin

Categories