Django - Automatically Assign Cleaned Data to Database Fields - python

The usual way I do form handling is like this:
contact = Contact(
name=form.cleaned_data['name'],
email=form.cleaned_data['email'],
message=form.cleaned_data['message'],
newsletter=form.cleaned_data['newsletter']
)
but for long forms, this obviously is a tedious task. I was wondering if there was any Django method I might have missed that automatically assigns the cleaned data to their corresponding column in the database table that I don't have to manually assign them.
Any ideas? Thanks in advance!

Django offers you a Modelform class that was desgined for exactly this task.

Related

Use autocomplete light with django-tables2

I'm trying to populate a django table with autocomplete light so that the user can fill in data in the table, which would then be saved (the whole table is in a form tag). I have the table working to display the existing data and I have the autocomplete working in model forms (well, a team member got that part working), but I don't know how to combine the two. The docs are a bit of a mystery to me, but maybe if someone could at least point me in the right direction I'd greatly appreciate it.
I've tried a few random things to combine them, but honestly they were such stabs in the dark that I don't think they're even worth mentioning.
tables.py
class ModifyTable(tables.Table):
name = tables.LinkColumn('app-view', args=[A('pk')], verbose_name='Name')
primary_contact = tables.Column()
secondary_contact = tables.Column()
autocomplete
autocomplete_light.register(Person,
search_fields=['first_name', 'last_name', 'username'],
split_words=True,
autocomplete_js_attributes={'placeholder': 'Find a user',},
)
Django-tables2 provides an API to generate data tables in HTML.
Django-autocomplete-light provides a widget that enables autocompletion inputs.
This widget must be used in a Form. The django Form class will combine your the HTML <form> with models used by django-tables2.
However, a Form must be used by a Formsets to be repeated for every row in the table. Note that you could consider modelformset_factory to generate such a formset.
Use a formset and your work is done here ;)

Can't create a form to upade part of a model field

This is really frustrating,
I can't and can't find how to create a form (I'm guessing a forms.Form form) to update just one field of a more complex model.
The model has 5 fields, and a form to create, update all of them.
But in a different case i need to let the user update only the title (a field in the model), so i need tried so many things until now (including creating an HTML form by hand and from the view to save it, creating a forms.Form and many more, nothing seem to work), There is no code here because i don't even know which one to put....
Maybe some one can help me with that, I'm sure it is a simple thing, But for some reason i am stuck on this for a long time...
Thank you,
Erez
If you're using ModelForms, you just have to define a fields attribute in Meta as a tuple containing just the names of the fields you want. See the documentation.

Get POST data from a complex Django form?

I have a Django form that uses a different number of fields based on the year/month. So I create the fields in the form like this:
for entry in entry_list:
self.fields[entry] = forms.DecimalField([stuffhere])
but now I don't know how to get the submitted data from the form.
Normally I would do something like:
form.cleaned_data["fieldname"]
but I don't know what the names of the fields are. The debug screen shows my POST data as simply "Entry Object" with a value of "u''". Calling POST.lists() doesn't show anything.
I am sure I am missing something obvious, but I've been stuck on this for a few days too many. Is there a better way to do this? Is all of the data in the request object, but I just don't know how to use it?
Here is the code for the model/form/view: http://pastebin.com/f28d92c0e
Much Thanks!
EDIT:
I've tried out both of the suggestions below. Using formsets was definitely easier and nicer.
I think you might be better off using formsets here. They're designed for exactly what you seem to be trying to do - dealing with a variable number of items within a form.
In this line:
self.fields[entry] = forms.DecimalField(max_digits=4, decimal_places=1, label=nice_label)
entry is a model instance. But fields are keyed by field names (strings). Try something like:
self.fields[entry.entry_name] = forms.Decimal(...)
(substitute appropriate for "entry_name").

how to use array in django

I have a db table which has an integer array. But how can I add this field in my model? I tried writing it using IntegerField but on save it is giving error
int() argument must be a string or a number, not 'list
How can I add this field to my model? I am using this field in my views.py so I need to add it in my model. Any suggestions?
You may be interested in using a CommaSeparatedIntegerField.
If you've got a list of integers like this:
my_ints = [1,2,3,4,5]
and a model like this:
class MyModel(models.Model):
values = CommaSeparatedIntegerField(max_length = 200)
then you can save my_ints into a MyModel like this:
m = MyModel(values = ','.join(my_ints))
m.save()
I would look into database normalization. In particular, your database is not even in 1st normal form, the first and probably most significant of the normal forms which states that normalized data should not contain any repeating groups. As a result, the Django object-relational-mapper will have considerable difficulty modeling your data.
By supporting only single, non-repeating types, Django in a sense enforces 1st normal form in data. You could try to write your own SQL to manage this particular field or perhaps find some code on the internet, but perhaps better would be to refactor this field into a many-to-one relationship in its own model. You can find Django documentation on this here.
Clueless' answer is probably the best you can get, but in case you still want to store array of numbers in single field, you can do this - either by manually e.g. pickling it and then storing to TextField, or by writing custom model field that do something like this for you automatically.
Here's the doc: http://docs.djangoproject.com/en/1.1/howto/custom-model-fields/
I got it working by saying textfield in my model.Since i am only using that field for reading it doesnot effect me

In Django, how can you change the User class to work with a different db table?

We're running django alongside - and sharing a database with - an existing application. And we want to use an existing "user" table (not Django's own) to store user information.
It looks like it's possible to change the name of the table that Django uses, in the Meta class of the User definition.
But we'd prefer not to change the Django core itself.
So we were thinking that we could sub-class the core auth.User class like this :
class OurUser(User) :
objects = UserManager()
class Meta:
db_table = u'our_user_table'
Here, the aim is not to add any extra fields to the customized User class. But just to use the alternative table.
However, this fails (likely because the ORM is assuming that the our_user_table should have a foreign key referring back to the original User table, which it doesn't).
So, is this sensible way to do what we want to do? Have I missed out on some easier way to map classes onto tables? Or, if not, can this be made to work?
Update :
I think I might be able to make the change I want just by "monkey-patching" the _meta of User in a local_settings.py
User._meta.db_table = 'our_user_table'
Can anyone think of anything bad that could happen if I do this? (Particularly in the context of a fairly typical Django / Pinax application?)
You might find it useful to set up your old table as an alternative authentication source and sidestep all these issues.
Another option is to subclass the user and have the subclass point to your user-model. Override the save function to ensure that everything you need to do to preserve your old functionality is there.
I haven't done either of these myself but hopefully they are useful pointers.
Update
What I mean by alternative authentication in this case is a small python script that says "Yes, this is a valid username / password" - It then creates an instance of model in the standard Django table, copies fields across from the legacy table and returns the new user to the caller.
If you need to keep the two tables in sync, you could decide to have your alternative authentication never create a standard django user and just say "Yes, this is a valid password and username"

Categories