On Wed, Jun 24, 2009 at 7:03 PM, Kusako<markus.strick...@googlemail.com> wrote:
>
> Hi-
>
> Is it possible to use models for objects that are not supposed to be
> persisted to the database, or read from it? I would like to use
> ModelForms, etc for them, but they should not have a table in the db
> or written to or read from the db.

If you just want a form, you don't need to use ModelForms - just use a
Form. The core Form framework operates independent of the database
layer - it is, quite literally, just a forms framework. ModelForm
exists as a convenience for building forms that are closely associated
with database objects, but you don't need the database layer, you
don't have to use them.

For example, if you want a form that will collect a name and age:

from django import forms
class MyForm(forms.Form):
    name = forms.CharField(max_length=100)
    age = forms.IntegerField()

You can then instantiate this form and check values. This form won't
have a save() method (since there is nothing to save), but it will
still have is_valid(), errors, cleaned_data, etc. The form itself will
be almost identical to the form produced if you had created a database
model:

from django.db import models
class MyModel(models.Model):
    name = models.CharField(max_length=100)
    age = models.IntegerField()

and then created a ModelForm:

from django import forms
class MyForm(forms.ModelForm):
    class Meta:
        model = MyModel

The parallels between the two syntaxes are very much intentional.

Yours,
Russ Magee %-)

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to