Django model fields get realigned after writing wrong data - django

In my Django admin panel, whenever I leave a field blank and save it the fields are realigned left to right and vice versa.
In the below pics 1st pic is when there is no data in the fields. 2nd pic is when i leave a field blank and save it the field has realigned from left to right.
Can anyone tell me what is the problem and how to fix this?
![Before writing anything in my panel[![After leaving a field blank][1]][1]][2]
[![After leaving a field blank][1]][1]

Related

Django: right-align column in admin listview

How can I output a DecimalField right-aligned in Django admin listview? It makes much more sense to have a column of, say, prices, right-aligned.
I tried this: django align right cannot align, but it doesn't work, presumably, because it doesn't change the output behaviour.

Editing of django form with primary key fields

I have a django model form for which I allowed partial form save since its a long form. Now I want the user to come back and complete the form later.
There is one hindrance however that my database cannot accept duplicate entries with same primary key. So should I remove primary key in my database to solve this or is there another way to make it possible? Suggestions please.
Why not keep only one row per user for that form? Just add one boolean field to check whether the form is submitted or is a draft. Something like is_submitted=models.Boolean(default=False). Keep updating the row with the new values on each partial save (a separate button for Save or an AJAX call for auto save).
When the form is submitted, just mark the field is_submitted = True and perform whatever actions you want after that.

Django: order formset without form change

I have a form with only one field (name of family members).
class FamilyMemeberItem(forms.Form):
name= forms.CharField(label=_('name'), max_length=20)
Now I want my form be sorted (arbitrary order) defined by the user. For example in a family, I want to show A first, then B and then C, while the creation sequence may be C, B and A. Is there anyway to do that?
I searched and realized I should add an order field to my form and override the __iter__() method. Is that the only way? If there is no way to do that without change in form?
And could anyone please tell me about the field can_order of formset_factory? When I add it, an extra filed is loaded next to my form, and that's and integer presenting the number of that field. Can I change and save that so that the order changes?
I answered a similar question you posted.
I'll just repeat the last part here:
If you want to store the order in the database, you need to define a new field in you model and store the order in that. The order of formsets is only present inside a single request/response, after that its gone.

Making hidden field visible when another field is used

I am trying to create a section in my models code that allows me to add a value only when the previous field is selected.
The goal of the code is to do the following
Have a null or blank option
Select an option from the tuple
Showing the new field option
The example of what I am looking to do is add a warranty to an item in my model. I am trying to make a date field (or a charfield that uses a date format) that only appears when an option is selected (i.e. a date field is available when the 30 day warranty is selected) otherwise the date field should be hidden (i.e. none is selected, so date field does not show)
Any suggestions, workarounds are welcomed
This is in Django Admin only

How to create linked (associated) automatically updated form fields in GAE Django

I am new to Django and GAE. I would like to create two input fields, where the first one is a drop-down menu (let name it select), which decides the values in the second one (let name it val).
For example, once 'A' is chosen from 'select', field 'val' will show '1'. similarly, 'B' is associated to '10'. I have written several lines below, but it does not work. Two issues:
The second field ('val') always equals 0.
It seems like my second field ('val') does not 'listen' to the choice made by the first one ('select'), which means those two fields are not linked.
Can anyone give me some suggestions (or recommend books on using Django on GAE)? Thank you!
select_CHOICES=(('A','A'),('B','B'),('Other','Other'))
select = forms.ChoiceField(choices=select_CHOICES, initial='A')
def get_choices(select):
if select=='A':
r= 1
elif select=='B':
r= 10
else:
r= 0
return r
val=forms.FloatField(initial=get_choices(select))
I think you have understood how django works a bit wrong. The code that you input is run before the page is rendered, so no selection is made yet. If you want the input field to dynamically change as user makes the choise on page, you should use Javascript.
Also you are comparing a Field (select) to a string ('A'), which naturally always is unequal.
Read more documentation and tutorials and you'll soon get how it works.