forms.ModelForm
I am using Django's ModelForm in a template im working on. I am trying to use it to display the Django User Object and an extension I added of the user model, what would be the correct way to display my User model extension in my template, I would like to be able to edit the User information and the account number I have added all from the same form. Thanks. class UserModelForm(forms.ModelForm): class Meta: model = User fields = ('username','first_name','last_name', 'password_field','email','account','is_superuser','is_staff') @staff_member_required def create_edit_users(request, id=None): user = request.user tick_num = Ticket.objects.filter(status__contains='Open').count() if id is not None: puser = get_object_or_404(User, pk=id) else: puser = None form = UserModelForm(data=request.POST or None, instance=puser) if form.is_valid(): form.save() return HttpResponseRedirect("/users/") return render_to_response("user.php", {'form':form},RequestContext (request)) My extension of the User model is as follows class Account_Number(models.Model): account_number = models.CharField(max_length=8) user = models.ForeignKey(User, unique=True) userfield = models.NullBooleanField(null=True, blank=True) def __str__(self): return self.account_number class Admin: list_display = ('account_number','userfield') --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
Re: forms.ModelForm
On Fri, 2009-03-06 at 12:25 -0800, mike171562 wrote: > I am using Django's ModelForm in a template im working on. I am trying > to use it to display the Django User Object and an extension I added > of the user model, what would be the correct way to display my User > model extension in my template, I would like to be able to edit the > User information and the account number I have added all from the same > form. Thanks. A Django Form object represents only a part of an HTML form (which is why need to write the HTML "form" tag in the template, for example). You can pass multiple form objects through from your view function to the template and render both of them inside the same HTML form. If you want to intermingle fields, you can define extra form fields on a model form. For example: class MyForm(forms.ModelForm): extra = forms.CharField(max_length=50) class Meta: model = models.Tag That will put the extra fields at the end of the model form. If you want to put the extra fields in the middle of the model fields somehow, you can either write a custom __init__ method to tweak the field ordering (you'll have to read the forms code a bit to see what needs tweaking). Alternatively, you can take the not unreasonable approach that you've gone beyond the scope of modelforms by this point and just write a normal Form class that contains the fields. I've always been an interested observer of people trying to make automatic model-related forms do all sorts of funky things, because I hardly ever use them. So many of my use-cases don't have forms mapping directly to models, so I write a normal Form class and then my view knows how to take fields from the Form instance and convert that data to the appropriate models. At some point, it's a fairly grief-free way to handle anything beyond the straight model -> form -> model conversion. But, as you can see from the above, you aren't short of options here. Regards, Malcolm --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
Re: forms.ModelForm
thanks malcolm, thats doesnt seem to work though class UserModelForm(forms.ModelForm): class Meta: model = User account = forms.CharField(max_length=50) fields = ('username','email','first_name','last_name','account') adding the 'account' field as extra is not displayed in the ((field)) form in the template. On Mar 6, 8:40 pm, Malcolm Tredinnick wrote: > On Fri, 2009-03-06 at 12:25 -0800, mike171562 wrote: > > I am using Django's ModelForm in a template im working on. I am trying > > to use it to display the Django User Object and an extension I added > > of the user model, what would be the correct way to display my User > > model extension in my template, I would like to be able to edit the > > User information and the account number I have added all from the same > > form. Thanks. > > A Django Form object represents only a part of an HTML form (which is > why need to write the HTML "form" tag in the template, for example). You > can pass multiple form objects through from your view function to the > template and render both of them inside the same HTML form. > > If you want to intermingle fields, you can define extra form fields on a > model form. For example: > > class MyForm(forms.ModelForm): > extra = forms.CharField(max_length=50) > > class Meta: > model = models.Tag > > That will put the extra fields at the end of the model form. > > If you want to put the extra fields in the middle of the model fields > somehow, you can either write a custom __init__ method to tweak the > field ordering (you'll have to read the forms code a bit to see what > needs tweaking). Alternatively, you can take the not unreasonable > approach that you've gone beyond the scope of modelforms by this point > and just write a normal Form class that contains the fields. > > I've always been an interested observer of people trying to make > automatic model-related forms do all sorts of funky things, because I > hardly ever use them. So many of my use-cases don't have forms mapping > directly to models, so I write a normal Form class and then my view > knows how to take fields from the Form instance and convert that data to > the appropriate models. At some point, it's a fairly grief-free way to > handle anything beyond the straight model -> form -> model conversion. > But, as you can see from the above, you aren't short of options here. > > Regards, > Malcolm --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
Re: forms.ModelForm
On Mon, Mar 9, 2009 at 10:10 AM, mike171562 wrote: > > thanks malcolm, thats doesnt seem to work though > > class UserModelForm(forms.ModelForm): >class Meta: >model = User > account = forms.CharField(max_length=50) >fields = > ('username','email','first_name','last_name','account') > adding the 'account' field as extra is not displayed in the ((field)) > form in the template. > > > > > On Mar 6, 8:40 pm, Malcolm Tredinnick > wrote: > > On Fri, 2009-03-06 at 12:25 -0800, mike171562 wrote: > > > I am using Django's ModelForm in a template im working on. I am trying > > > to use it to display the Django User Object and an extension I added > > > of the user model, what would be the correct way to display my User > > > model extension in my template, I would like to be able to edit the > > > User information and the account number I have added all from the same > > > form. Thanks. > > > > A Django Form object represents only a part of an HTML form (which is > > why need to write the HTML "form" tag in the template, for example). You > > can pass multiple form objects through from your view function to the > > template and render both of them inside the same HTML form. > > > > If you want to intermingle fields, you can define extra form fields on a > > model form. For example: > > > > class MyForm(forms.ModelForm): > > extra = forms.CharField(max_length=50) > > > > class Meta: > > model = models.Tag > > > > That will put the extra fields at the end of the model form. > > > > If you want to put the extra fields in the middle of the model fields > > somehow, you can either write a custom __init__ method to tweak the > > field ordering (you'll have to read the forms code a bit to see what > > needs tweaking). Alternatively, you can take the not unreasonable > > approach that you've gone beyond the scope of modelforms by this point > > and just write a normal Form class that contains the fields. > > > > I've always been an interested observer of people trying to make > > automatic model-related forms do all sorts of funky things, because I > > hardly ever use them. So many of my use-cases don't have forms mapping > > directly to models, so I write a normal Form class and then my view > > knows how to take fields from the Form instance and convert that data to > > the appropriate models. At some point, it's a fairly grief-free way to > > handle anything beyond the straight model -> form -> model conversion. > > But, as you can see from the above, you aren't short of options here. > > > > Regards, > > Malcolm > > > The account = part shouldn't be in the inner Meta class. Alex -- "I disapprove of what you say, but I will defend to the death your right to say it." --Voltaire "The people's good is the highest law."--Cicero --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
Re: forms.ModelForm
Thanks alex, that worked, I have the extra field, and now to figure out how to to tie it in to the main form and my user model. On Mar 9, 10:12 am, Alex Gaynor wrote: > On Mon, Mar 9, 2009 at 10:10 AM, mike171562 wrote: > > > > > > > thanks malcolm, thats doesnt seem to work though > > > class UserModelForm(forms.ModelForm): > > class Meta: > > model = User > > account = forms.CharField(max_length=50) > > fields = > > ('username','email','first_name','last_name','account') > > adding the 'account' field as extra is not displayed in the ((field)) > > form in the template. > > > On Mar 6, 8:40 pm, Malcolm Tredinnick > > wrote: > > > On Fri, 2009-03-06 at 12:25 -0800, mike171562 wrote: > > > > I am using Django's ModelForm in a template im working on. I am trying > > > > to use it to display the Django User Object and an extension I added > > > > of the user model, what would be the correct way to display my User > > > > model extension in my template, I would like to be able to edit the > > > > User information and the account number I have added all from the same > > > > form. Thanks. > > > > A Django Form object represents only a part of an HTML form (which is > > > why need to write the HTML "form" tag in the template, for example). You > > > can pass multiple form objects through from your view function to the > > > template and render both of them inside the same HTML form. > > > > If you want to intermingle fields, you can define extra form fields on a > > > model form. For example: > > > > class MyForm(forms.ModelForm): > > > extra = forms.CharField(max_length=50) > > > > class Meta: > > > model = models.Tag > > > > That will put the extra fields at the end of the model form. > > > > If you want to put the extra fields in the middle of the model fields > > > somehow, you can either write a custom __init__ method to tweak the > > > field ordering (you'll have to read the forms code a bit to see what > > > needs tweaking). Alternatively, you can take the not unreasonable > > > approach that you've gone beyond the scope of modelforms by this point > > > and just write a normal Form class that contains the fields. > > > > I've always been an interested observer of people trying to make > > > automatic model-related forms do all sorts of funky things, because I > > > hardly ever use them. So many of my use-cases don't have forms mapping > > > directly to models, so I write a normal Form class and then my view > > > knows how to take fields from the Form instance and convert that data to > > > the appropriate models. At some point, it's a fairly grief-free way to > > > handle anything beyond the straight model -> form -> model conversion. > > > But, as you can see from the above, you aren't short of options here. > > > > Regards, > > > Malcolm > > The account = part shouldn't be in the inner Meta class. > > Alex > > -- > "I disapprove of what you say, but I will defend to the death your right to > say it." --Voltaire > "The people's good is the highest law."--Cicero --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
Re: forms.ModelForm
To display and edit my extension of the user model in the admin interface I simply add class Admin: list_display = ('account_number','userfield') how can i get this to display in my ModelForm fields? On Mar 9, 10:51 am, mike171562 wrote: > Thanks alex, that worked, I have the extra field, and now to figure > out how to to tie it in to the main form and my user model. > > On Mar 9, 10:12 am, Alex Gaynor wrote: > > > On Mon, Mar 9, 2009 at 10:10 AM, mike171562 > > wrote: > > > > thanks malcolm, thats doesnt seem to work though > > > > class UserModelForm(forms.ModelForm): > > > class Meta: > > > model = User > > > account = forms.CharField(max_length=50) > > > fields = > > > ('username','email','first_name','last_name','account') > > > adding the 'account' field as extra is not displayed in the ((field)) > > > form in the template. > > > > On Mar 6, 8:40 pm, Malcolm Tredinnick > > > wrote: > > > > On Fri, 2009-03-06 at 12:25 -0800, mike171562 wrote: > > > > > I am using Django's ModelForm in a template im working on. I am trying > > > > > to use it to display the Django User Object and an extension I added > > > > > of the user model, what would be the correct way to display my User > > > > > model extension in my template, I would like to be able to edit the > > > > > User information and the account number I have added all from the same > > > > > form. Thanks. > > > > > A Django Form object represents only a part of an HTML form (which is > > > > why need to write the HTML "form" tag in the template, for example). You > > > > can pass multiple form objects through from your view function to the > > > > template and render both of them inside the same HTML form. > > > > > If you want to intermingle fields, you can define extra form fields on a > > > > model form. For example: > > > > > class MyForm(forms.ModelForm): > > > > extra = forms.CharField(max_length=50) > > > > > class Meta: > > > > model = models.Tag > > > > > That will put the extra fields at the end of the model form. > > > > > If you want to put the extra fields in the middle of the model fields > > > > somehow, you can either write a custom __init__ method to tweak the > > > > field ordering (you'll have to read the forms code a bit to see what > > > > needs tweaking). Alternatively, you can take the not unreasonable > > > > approach that you've gone beyond the scope of modelforms by this point > > > > and just write a normal Form class that contains the fields. > > > > > I've always been an interested observer of people trying to make > > > > automatic model-related forms do all sorts of funky things, because I > > > > hardly ever use them. So many of my use-cases don't have forms mapping > > > > directly to models, so I write a normal Form class and then my view > > > > knows how to take fields from the Form instance and convert that data to > > > > the appropriate models. At some point, it's a fairly grief-free way to > > > > handle anything beyond the straight model -> form -> model conversion. > > > > But, as you can see from the above, you aren't short of options here. > > > > > Regards, > > > > Malcolm > > > The account = part shouldn't be in the inner Meta class. > > > Alex > > > -- > > "I disapprove of what you say, but I will defend to the death your right to > > say it." --Voltaire > > "The people's good is the highest law."--Cicero --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
Re: forms.ModelForm
On Mon, Mar 9, 2009 at 10:57 AM, mike171562 wrote: > > To display and edit my extension of the user model in the admin > interface I simply add > >class Admin: >list_display = ('account_number','userfield') > > how can i get this to display in my ModelForm fields? > > > > > > On Mar 9, 10:51 am, mike171562 wrote: > > Thanks alex, that worked, I have the extra field, and now to figure > > out how to to tie it in to the main form and my user model. > > > > On Mar 9, 10:12 am, Alex Gaynor wrote: > > > > > On Mon, Mar 9, 2009 at 10:10 AM, mike171562 < > support.desk@gmail.com>wrote: > > > > > > thanks malcolm, thats doesnt seem to work though > > > > > > class UserModelForm(forms.ModelForm): > > > >class Meta: > > > >model = User > > > > account = forms.CharField(max_length=50) > > > >fields = > > > > ('username','email','first_name','last_name','account') > > > > adding the 'account' field as extra is not displayed in the ((field)) > > > > form in the template. > > > > > > On Mar 6, 8:40 pm, Malcolm Tredinnick > > > > wrote: > > > > > On Fri, 2009-03-06 at 12:25 -0800, mike171562 wrote: > > > > > > I am using Django's ModelForm in a template im working on. I am > trying > > > > > > to use it to display the Django User Object and an extension I > added > > > > > > of the user model, what would be the correct way to display my > User > > > > > > model extension in my template, I would like to be able to edit > the > > > > > > User information and the account number I have added all from the > same > > > > > > form. Thanks. > > > > > > > A Django Form object represents only a part of an HTML form (which > is > > > > > why need to write the HTML "form" tag in the template, for > example). You > > > > > can pass multiple form objects through from your view function to > the > > > > > template and render both of them inside the same HTML form. > > > > > > > If you want to intermingle fields, you can define extra form fields > on a > > > > > model form. For example: > > > > > > > class MyForm(forms.ModelForm): > > > > > extra = forms.CharField(max_length=50) > > > > > > > class Meta: > > > > > model = models.Tag > > > > > > > That will put the extra fields at the end of the model form. > > > > > > > If you want to put the extra fields in the middle of the model > fields > > > > > somehow, you can either write a custom __init__ method to tweak the > > > > > field ordering (you'll have to read the forms code a bit to see > what > > > > > needs tweaking). Alternatively, you can take the not unreasonable > > > > > approach that you've gone beyond the scope of modelforms by this > point > > > > > and just write a normal Form class that contains the fields. > > > > > > > I've always been an interested observer of people trying to make > > > > > automatic model-related forms do all sorts of funky things, because > I > > > > > hardly ever use them. So many of my use-cases don't have forms > mapping > > > > > directly to models, so I write a normal Form class and then my view > > > > > knows how to take fields from the Form instance and convert that > data to > > > > > the appropriate models. At some point, it's a fairly grief-free way > to > > > > > handle anything beyond the straight model -> form -> model > conversion. > > > > > But, as you can see from the above, you aren't short of options > here. > > > > > > > Regards, > > > > > Malcolm > > > > > The account = part shouldn't be in the inner Meta class. > > > > > Alex > > > > > -- > > > "I disapprove of what you say, but I will defend to the death your > right to > > > say it." --Voltaire > > > "The people's good is the highest law."--Cicero > > > Look at the fields options on the ModelAdmin: http://docs.djangoproject.com/en/dev/ref/contrib/admin/#fields or the form option if you want to supply your own form class. Alex -- "I disapprove of what you say, but I will defend to the death your right to say it." --Voltaire "The people's good is the highest law."--Cicero --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
newforms-admin, forms.ModelForm, formtools.preview
Has anyone successfully got formtools.preview working with forms.ModelForm so that the form fields are populated from the model. If so, do you have an example?? --~--~-~--~~~---~--~~ 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
forms.Form, models.Model, forms.ModelForm? (self.django)
Would someone explain to me when you would choose one class over the other? Does forms.Form not create a database? Can't you just render models.Model as a form? I understand forms.ModelForm is a helper class for creating a form from a model. But I still don't really understand why you would choose one over the other, like when? Are forms.Form more for GET requests? And the others are POST? Most of what I am trying to do is serve some forms, have the user POST some data, and then query the database. Thanks guys. -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To post to this group, send email to django-users@googlegroups.com. Visit this group at https://groups.google.com/group/django-users. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/0db5cf5f-23d2-43c1-8d1c-87ce5eab7efe%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.
Re: newforms-admin, forms.ModelForm, formtools.preview
On Mon, Jul 7, 2008 at 6:22 AM, d-rave <[EMAIL PROTECTED]> wrote: > > Has anyone successfully got formtools.preview working with > forms.ModelForm so that the form fields are populated from the model. > > If so, do you have an example?? Dave, I'm not familiar with formtools but if all you want to do is prepopulate a form then you can use the initial={} keyword arg. This probably doesn't help but ... you could do this in your view if you want: form = Form(initial={'user':u, ...}) http://www.djangoproject.com/documentation/newforms/#dynamic-initial-values -- Milan --~--~-~--~~~---~--~~ 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: newforms-admin, forms.ModelForm, formtools.preview
On Tue, Jul 8, 2008 at 10:00 AM, Milan Andric <[EMAIL PROTECTED]> wrote: > On Mon, Jul 7, 2008 at 6:22 AM, d-rave <[EMAIL PROTECTED]> wrote: >> >> Has anyone successfully got formtools.preview working with >> forms.ModelForm so that the form fields are populated from the model. >> >> If so, do you have an example?? > > Dave, I'm not familiar with formtools but if all you want to do is > prepopulate a form then you can use the initial={} keyword arg. This > probably doesn't help but ... you could do this in your view if you > want: > > form = Form(initial={'user':u, ...}) > > http://www.djangoproject.com/documentation/newforms/#dynamic-initial-values > Another trick is to use model_to_dict to pass in initial values : from django.newforms.models import model_to_dict ... f = Form( initial=model_to_dict(obj) ) -- Milan --~--~-~--~~~---~--~~ 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: newforms-admin, forms.ModelForm, formtools.preview
Thanks very much for these responses, they've given me something to think about. However, assuming I'm going about things the right way, the problem appears with the loading of Model data into a Form object which can be passed to a FormTools.preview class. For example, in the view function view_post() I thought I'd to have something like this view.py def view_post(request, post_id) post_obj = get_object_or_404(Post, pk=post_id) post_form = PostForm(instance=post_obj) # PostForm being a models.ModelForm then having something like return PostFormPreview(post_form) forms.py class PostForm(models.ModelForm): title=fields.CharField(required=False) body=fields.TextField(required=True) def save(): ... class PostFormPreview(preview.FormPreview): def parse_params(self, *args, **kwargs): ... def done(self, request, cleaned_data): # The Save() process begins here But this is were my problems begin. Amongst the comments of formtools/ preview.py:- class FormPreview(object): preview_template = 'formtools/preview.html' form_template = 'formtools/form.html' # METHODS SUBCLASSES SHOULDN'T OVERRIDE ### def __init__(self, form): # form should be a Form class, not an instance. "form should be a Form class, not an instance." Additionally, in the view_post function in view.py, by returning PostFormPreview causes the following error:- AttributeError: 'PostFormPreview' object has no attribute 'status_code' If I remove the "return" from the view_post, django complains view_post didn't return a HttpResponse object. On Jul 8, 4:05 pm, "Milan Andric" <[EMAIL PROTECTED]> wrote: > On Tue, Jul 8, 2008 at 10:00 AM, Milan Andric <[EMAIL PROTECTED]> wrote: > > On Mon, Jul 7, 2008 at 6:22 AM, d-rave <[EMAIL PROTECTED]> wrote: > > >> Has anyone successfully got formtools.preview working with > >> forms.ModelForm so that the form fields are populated from the model. > > >> If so, do you have an example?? > > > Dave, I'm not familiar with formtools but if all you want to do is > > prepopulate a form then you can use the initial={} keyword arg. This > > probably doesn't help but ... you could do this in your view if you > > want: > > > form = Form(initial={'user':u, ...}) > > >http://www.djangoproject.com/documentation/newforms/#dynamic-initial-... > > Another trick is to use model_to_dict to pass in initial values : > > from django.newforms.models import model_to_dict > ... > f = Form( initial=model_to_dict(obj) ) > > -- > Milan --~--~-~--~~~---~--~~ 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: forms.Form, models.Model, forms.ModelForm? (self.django)
forms.Form makes front-end widgets and some validation logic. models.Model defines a database interface and wraps it in a Python class. The two have nothing to do with each other aside from vaguely similar APIs for defining the classes. The intersection of the two is forms.ModelForm, which uses a model to populate form fields. GET vs POST is a functionality difference. GET should be used where the request could be repeated multiple times and get the same response. POST should be used where a request has meaningful side effects. (If you're saving things to a model from a user perspective, you almost certainly want POST.) There's middle ground between the two where it's a bit of a matter of opinion, but those are the broad strokes. On Sun, Aug 14, 2016 at 2:04 PM, Andrew Emory wrote: > Would someone explain to me when you would choose one class over the > other? Does forms.Form not create a database? Can't you just render > models.Model as a form? > > > I understand forms.ModelForm is a helper class for creating a form from a > model. But I still don't really understand why you would choose one over > the other, like when? > > > Are forms.Form more for GET requests? And the others are POST? > > > Most of what I am trying to do is serve some forms, have the user POST > some data, and then query the database. > > > Thanks guys. > > -- > You received this message because you are subscribed to the Google Groups > "Django users" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to django-users+unsubscr...@googlegroups.com. > To post to this group, send email to django-users@googlegroups.com. > Visit this group at https://groups.google.com/group/django-users. > To view this discussion on the web visit https://groups.google.com/d/ > msgid/django-users/0db5cf5f-23d2-43c1-8d1c-87ce5eab7efe%40googlegroups.com > <https://groups.google.com/d/msgid/django-users/0db5cf5f-23d2-43c1-8d1c-87ce5eab7efe%40googlegroups.com?utm_medium=email&utm_source=footer> > . > For more options, visit https://groups.google.com/d/optout. > -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To post to this group, send email to django-users@googlegroups.com. Visit this group at https://groups.google.com/group/django-users. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CA%2Bv0ZYUvnf1p-GCa%3DyrHUfGLORQeSDeBbBnUaDCrvdyEPR5wCQ%40mail.gmail.com. For more options, visit https://groups.google.com/d/optout.
Re: forms.Form, models.Model, forms.ModelForm? (self.django)
Thanks so much. This is exactly the kind of stuff I wanted to hear. It was just unclear if form.Form created a model when you define it or if it was just for GET data. So models are for the database and the admin. You can render them by way of forms.ModelForm which is what you would use for client facing POST requests? On Sunday, August 14, 2016 at 6:35:49 PM UTC-5, Alex Heyden wrote: > > forms.Form makes front-end widgets and some validation logic. models.Model > defines a database interface and wraps it in a Python class. The two have > nothing to do with each other aside from vaguely similar APIs for defining > the classes. > > The intersection of the two is forms.ModelForm, which uses a model to > populate form fields. > > GET vs POST is a functionality difference. GET should be used where the > request could be repeated multiple times and get the same response. POST > should be used where a request has meaningful side effects. (If you're > saving things to a model from a user perspective, you almost certainly want > POST.) There's middle ground between the two where it's a bit of a matter > of opinion, but those are the broad strokes. > > On Sun, Aug 14, 2016 at 2:04 PM, Andrew Emory > wrote: > >> Would someone explain to me when you would choose one class over the >> other? Does forms.Form not create a database? Can't you just render >> models.Model as a form? >> >> >> I understand forms.ModelForm is a helper class for creating a form from a >> model. But I still don't really understand why you would choose one over >> the other, like when? >> >> >> Are forms.Form more for GET requests? And the others are POST? >> >> >> Most of what I am trying to do is serve some forms, have the user POST >> some data, and then query the database. >> >> >> Thanks guys. >> >> -- >> You received this message because you are subscribed to the Google Groups >> "Django users" group. >> To unsubscribe from this group and stop receiving emails from it, send an >> email to django-users...@googlegroups.com . >> To post to this group, send email to django...@googlegroups.com >> . >> Visit this group at https://groups.google.com/group/django-users. >> To view this discussion on the web visit >> https://groups.google.com/d/msgid/django-users/0db5cf5f-23d2-43c1-8d1c-87ce5eab7efe%40googlegroups.com >> >> <https://groups.google.com/d/msgid/django-users/0db5cf5f-23d2-43c1-8d1c-87ce5eab7efe%40googlegroups.com?utm_medium=email&utm_source=footer> >> . >> For more options, visit https://groups.google.com/d/optout. >> > > -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To post to this group, send email to django-users@googlegroups.com. Visit this group at https://groups.google.com/group/django-users. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/7ebd0d58-7aea-4866-97fe-1cd5ebd1654d%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.
How to save bio using forms.ModelForm
I tried multiple links on stackov. but none worked, I can't get my *bio* form to save the data on the db for the user profile :/ I managed to save the first name and last name, but I can't make the bio save... This is my code: *profile.html* {% csrf_token %} {{ user_BioAndSocialForm.bio |as_crispy_field }} Update *views.py* from django.shortcuts import render, redirect from django.contrib import messages # to display alert messages when the form data is valid from .forms import UserSignUpForm, UserUpdateForm, ProfileUpdateForm, UserProfileForm, BioAndSocialForm from django.contrib.auth.decorators import login_required from django.contrib.auth import update_session_auth_hash from django.http import HttpResponseRedirect from django.shortcuts import HttpResponse from django.contrib.auth.forms import PasswordChangeForm @login_required def profile(request): if request.method == 'POST': user_ProfileForm = UserProfileForm(request.POST, instance=request.user) user_BioAndSocialForm = BioAndSocialForm(request.POST, instance= request.user) if user_ProfileForm.is_valid() and user_BioAndSocialForm.is_valid(): user_ProfileForm.save() user_BioAndSocialForm.save() messages.success(request, f'Profile updated!') return HttpResponseRedirect(request.path_info) else: messages.error(request, _('Please correct the error below.')) else: user_ProfileForm = UserProfileForm(instance=request.user) user_BioAndSocialForm = BioAndSocialForm(instance=request.user) context = { 'user_ProfileForm': user_ProfileForm, 'user_BioAndSocialForm': user_BioAndSocialForm } return render(request, 'users/profile.html', context) *forms.py* from django import forms from django.contrib.auth.models import User from django.contrib.auth.forms import UserCreationForm from .models import Profile from crispy_forms.helper import FormHelper class BioAndSocialForm(forms.ModelForm): class Meta: model = Profile fields = ['bio'] def __init__(self, *args, **kwargs): super(BioAndSocialForm, self).__init__(*args, **kwargs) self.helper = FormHelper() self.helper.form_show_labels = False *models.py* from django.db import models from django.contrib.auth.models import User from PIL import Image # Profle model with data regarding the user class Profile(models.Model): user = models.OneToOneField(User, on_delete=models.CASCADE) bio = models.TextField(max_length=500, blank=True) birth_date = models.DateField(null=True, blank=True) #Image feature upload image = models.ImageField(default='default.jpg', upload_to='profile_pics') # If we don't have this, it's going to say profile object only def __str__(self): return f'{self.user.username} Profile' # it's going to print username Profile def save(self, *args, **kwargs): super().save(*args, **kwargs) img = Image.open(self.image.path) if img.height > 300 or img.width > 300: output_size = (300, 300) img.thumbnail(output_size) img.save(self.image.path) This is what the view looks like: When I hit update, and it refreshes, the bio info is not there. Could someone please help me? Thank you -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To post to this group, send email to django-users@googlegroups.com. Visit this group at https://groups.google.com/group/django-users. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/85fec521-bc2d-444d-8b81-404389e81138%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.
Django : InlineFomsets with FileField, overwrite forms.ModelForm Function
I have a model Product and a model Document. The document has a foreign key to the product. I want the user to upload multiple files in the same form for adding/editing Product. I modified and example from net, it is working, but not with a FileField. I know that I need to add request.File but until now I didn't added where is needed, because is not working. *models* class Product( models.Model): #default attributes name = models.CharField(max_length=200,db_index=True) short_description = models.CharField(max_length=160) description = models.TextField() class Document(models.Model): product = models.ForeignKey('products.Product', on_delete=models.CASCADE) document = models.FileField(upload_to=upload_to) *forms* class DocumentModelForm(forms.ModelForm): class Meta: model = Document fields = ['document'] DocumentInlineFormSet = forms.inlineformset_factory( Product, Document, fields=('document', 'id'), extra=0, can_delete=False, min_num=1, validate_min=True, max_num=3, validate_max=True, ) class ProductModelForm(forms.ModelForm): class Meta: model = Product fields = [ 'name', 'short_description', 'description', ] def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.checks = DocumentInlineFormSet( instance=self.instance, prefix='checks', data=self.data if self.is_bound else None) def is_valid(self, commit=True): product_valid = super().is_valid() checks_valid = self.checks.is_valid() return product_valid and checks_valid def save(self,commit=True): product = super().save(commit=commit) product._checks = self.checks.save(commit=commit) return product *template* {% csrf_token %} {{ form}} {{ form.checks.management_form }} {{ form.checks }} *views are normal* -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To post to this group, send email to django-users@googlegroups.com. Visit this group at https://groups.google.com/group/django-users. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/eb6d1b67-ad71-42fd-a826-af2669c470a1%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.
how to save a forms.ModelForm with extra fields?
Hi I've got the Rating model and the AddRestaurantForm shown below. In order to add a rating when submitting a new restaurant, I added an extra field to AddRestaurantForm. Can I do that? If I can, how can I save separately the Restaurant instance and the rating instance (I'll get the user from the context)? thanks jul class Rating(models.Model): user = models.ForeignKey(User) restaurant = models.ForeignKey(Restaurant) rating = models.PositiveSmallIntegerField() class AddRestaurantForm(ModelForm): rating = models.IntegerField() class Meta: model = Restaurant -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@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=.
Re: how to save a forms.ModelForm with extra fields?
On 09-11-12 2:33 PM, jul wrote: > I've got the Rating model and the AddRestaurantForm shown below. In > order to add a rating when submitting a new restaurant, I added an > extra field to AddRestaurantForm. Can I do that? If I can, how can I > save separately the Restaurant instance and the rating instance (I'll > get the user from the context)? Sure that works just fine. When you save the form, you'll save the restaurant instance. You can then get the rating from the forms.cleaned_data and save that however you'd like. -- Andy McKay @clearwind Training: http://clearwind.ca/training/ Zen: http://djangozen.com -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@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=.
Re: how to save a forms.ModelForm with extra fields?
You have a rating field in your model, I don't know if it is intentionally but then in your ModelForm, if you specify an extra field also named 'rating' then you are overriding the one in the model, I think. Also, such extra field in the form, shouldn't it be form.IntegerField instead of model.IntegerField? Hector Garcia - Web developer, musician http://nomadblue.com/ On Fri, Nov 13, 2009 at 5:35 AM, Andy Mckay wrote: > On 09-11-12 2:33 PM, jul wrote: >> I've got the Rating model and the AddRestaurantForm shown below. In >> order to add a rating when submitting a new restaurant, I added an >> extra field to AddRestaurantForm. Can I do that? If I can, how can I >> save separately the Restaurant instance and the rating instance (I'll >> get the user from the context)? > > Sure that works just fine. > > When you save the form, you'll save the restaurant instance. You can > then get the rating from the forms.cleaned_data and save that however > you'd like. > -- > Andy McKay > @clearwind > Training: http://clearwind.ca/training/ > Zen: http://djangozen.com > > -- > > You received this message because you are subscribed to the Google Groups > "Django users" group. > To post to this group, send email to django-us...@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=. > > > -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@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=.
Re: how to save a forms.ModelForm with extra fields?
yes you're right it should be rating = models.IntegerField(). In my Restaurant model there is no rating field, so it's not overrided. thanks On Nov 13, 11:16 am, Hector Garcia wrote: > You have a rating field in your model, I don't know if it is > intentionally but then in your ModelForm, if you specify an extra > field also named 'rating' then you are overriding the one in the > model, I think. Also, such extra field in the form, shouldn't it be > form.IntegerField instead of model.IntegerField? > > Hector Garcia - Web developer, musicianhttp://nomadblue.com/ > > On Fri, Nov 13, 2009 at 5:35 AM, Andy Mckay wrote: > > On 09-11-12 2:33 PM, jul wrote: > >> I've got the Rating model and the AddRestaurantForm shown below. In > >> order to add a rating when submitting a new restaurant, I added an > >> extra field to AddRestaurantForm. Can I do that? If I can, how can I > >> save separately the Restaurant instance and the rating instance (I'll > >> get the user from the context)? > > > Sure that works just fine. > > > When you save the form, you'll save the restaurant instance. You can > > then get the rating from the forms.cleaned_data and save that however > > you'd like. > > -- > > Andy McKay > > @clearwind > > Training:http://clearwind.ca/training/ > > Zen:http://djangozen.com > > > -- > > > You received this message because you are subscribed to the Google Groups > > "Django users" group. > > To post to this group, send email to django-us...@googlegroups.com. > > To unsubscribe from this group, send email to > > django-users+unsubscr...@googlegroups.com. > > For more options, visit this group > > athttp://groups.google.com/group/django-users?hl=. -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@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=.
Re: how to save a forms.ModelForm with extra fields?
I meant rating = forms.IntegerField() On Nov 13, 11:16 am, Hector Garcia wrote: > You have a rating field in your model, I don't know if it is > intentionally but then in your ModelForm, if you specify an extra > field also named 'rating' then you are overriding the one in the > model, I think. Also, such extra field in the form, shouldn't it be > form.IntegerField instead of model.IntegerField? > > Hector Garcia - Web developer, musicianhttp://nomadblue.com/ > > On Fri, Nov 13, 2009 at 5:35 AM, Andy Mckay wrote: > > On 09-11-12 2:33 PM, jul wrote: > >> I've got the Rating model and the AddRestaurantForm shown below. In > >> order to add a rating when submitting a new restaurant, I added an > >> extra field to AddRestaurantForm. Can I do that? If I can, how can I > >> save separately the Restaurant instance and the rating instance (I'll > >> get the user from the context)? > > > Sure that works just fine. > > > When you save the form, you'll save the restaurant instance. You can > > then get the rating from the forms.cleaned_data and save that however > > you'd like. > > -- > > Andy McKay > > @clearwind > > Training:http://clearwind.ca/training/ > > Zen:http://djangozen.com > > > -- > > > You received this message because you are subscribed to the Google Groups > > "Django users" group. > > To post to this group, send email to django-us...@googlegroups.com. > > To unsubscribe from this group, send email to > > django-users+unsubscr...@googlegroups.com. > > For more options, visit this group > > athttp://groups.google.com/group/django-users?hl=. -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@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=.
Re: how to save a forms.ModelForm with extra fields?
What happen to the rating field when doing the following? f = AddRestaurantForm(request.POST) f.save() Does save() only use what it needs to fill the Restaurant instance and doesn't use the rating value? On Nov 13, 5:35 am, Andy Mckay wrote: > On 09-11-12 2:33 PM, jul wrote: > > > I've got the Rating model and the AddRestaurantForm shown below. In > > order to add a rating when submitting a new restaurant, I added an > > extra field to AddRestaurantForm. Can I do that? If I can, how can I > > save separately the Restaurant instance and the rating instance (I'll > > get the user from the context)? > > Sure that works just fine. > > When you save the form, you'll save the restaurant instance. You can > then get the rating from the forms.cleaned_data and save that however > you'd like. > -- > Andy McKay > �...@clearwind > Training:http://clearwind.ca/training/ > Zen:http://djangozen.com -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@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=.
Re: how to save a forms.ModelForm with extra fields?
Exactly, it ignores whatever POST value not related to the model field. You have to use f.cleaned_data['rating'] to get the rating value and use it or save it wherever it has to be stored, as Andy McKay pointed in his comment. Hector Garcia - Web developer, musician http://nomadblue.com/ On Fri, Nov 13, 2009 at 11:59 AM, jul wrote: > What happen to the rating field when doing the following? > > f = AddRestaurantForm(request.POST) > f.save() > > Does save() only use what it needs to fill the Restaurant instance and > doesn't use the rating value? > > > On Nov 13, 5:35 am, Andy Mckay wrote: >> On 09-11-12 2:33 PM, jul wrote: >> >> > I've got the Rating model and the AddRestaurantForm shown below. In >> > order to add a rating when submitting a new restaurant, I added an >> > extra field to AddRestaurantForm. Can I do that? If I can, how can I >> > save separately the Restaurant instance and the rating instance (I'll >> > get the user from the context)? >> >> Sure that works just fine. >> >> When you save the form, you'll save the restaurant instance. You can >> then get the rating from the forms.cleaned_data and save that however >> you'd like. >> -- >> Andy McKay >> �...@clearwind >> Training:http://clearwind.ca/training/ >> Zen:http://djangozen.com > > -- > > You received this message because you are subscribed to the Google Groups > "Django users" group. > To post to this group, send email to django-us...@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=. > > > -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@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=.
Re: how to save a forms.ModelForm with extra fields?
great. Thank you! On Nov 13, 1:42 pm, Hector Garcia wrote: > Exactly, it ignores whatever POST value not related to the model > field. You have to use f.cleaned_data['rating'] to get the rating > value and use it or save it wherever it has to be stored, as Andy > McKay pointed in his comment. > > Hector Garcia - Web developer, musicianhttp://nomadblue.com/ > > On Fri, Nov 13, 2009 at 11:59 AM, jul wrote: > > What happen to the rating field when doing the following? > > > f = AddRestaurantForm(request.POST) > > f.save() > > > Does save() only use what it needs to fill the Restaurant instance and > > doesn't use the rating value? > > > On Nov 13, 5:35 am, Andy Mckay wrote: > >> On 09-11-12 2:33 PM, jul wrote: > > >> > I've got the Rating model and the AddRestaurantForm shown below. In > >> > order to add a rating when submitting a new restaurant, I added an > >> > extra field to AddRestaurantForm. Can I do that? If I can, how can I > >> > save separately the Restaurant instance and the rating instance (I'll > >> > get the user from the context)? > > >> Sure that works just fine. > > >> When you save the form, you'll save the restaurant instance. You can > >> then get the rating from the forms.cleaned_data and save that however > >> you'd like. > >> -- > >> Andy McKay > >> �...@clearwind > >> Training:http://clearwind.ca/training/ > >> Zen:http://djangozen.com > > > -- > > > You received this message because you are subscribed to the Google Groups > > "Django users" group. > > To post to this group, send email to django-us...@googlegroups.com. > > To unsubscribe from this group, send email to > > django-users+unsubscr...@googlegroups.com. > > For more options, visit this group > > athttp://groups.google.com/group/django-users?hl=. -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@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=.
overriding __init__ in forms.ModelForm is erroring with POST method
Hi All, I am working on small hands-on project.. and having an issue when calling the POST method with form.py overriding __init__. Below is the code. Here i have two models.. one is Category and another is SubCategory. For SubCategory i have created form "SubCategoryFormv2" in forms.py... where i am checking if categoryid is passed then pre-select that Category, else display all Category for selection. On submit the form from html... i am getting TypeError. (full error is displayed below) Please help... What i could identify is with the POST method, when the statement *form = SubCategoryFormv2(request.POST)* is executed, it is expecting some id.. not sure what is this id. # models.py class SubCategory(models.Model) : category = models.ForeignKey(Category, null = True, on_delete = models.SET_NULL) name = models.CharField(max_length = 200, null = True) dateCreated = models.DateTimeField(default = timezone.now) createdBy = models.ForeignKey(User, on_delete = models.PROTECT ) # forms.py -- class SubCategoryFormv2(forms.ModelForm): class Meta: model = SubCategory fields = '__all__' widgets = {'dateCreated' : DateInput()} def __init__(self, categoryid=None, *args, **kwargs): super(SubCategoryFormv2, self).__init__(*args, **kwargs) if categoryid is not None: self.fields['category'] = forms.ModelChoiceField(initial=categoryid, queryset=Category.objects.filter(id=categoryid)) # views.py def createSubCategoryV2(request, categoryid=None): context = { 'title':'Add SubCategory' } if request.method =='POST': print('POST METHOD') form = SubCategoryFormv2(request.POST) print('POST METHOD - SubCategoryFormv2 called') if form.is_valid(): ins = form.save(commit=True) print('Inside for form validation') messages.success(request, f'SubCategory added Successfully') return redirect('listsubcategory') else: # for GET method. print('GET METHOD') form = SubCategoryFormv2(categoryid) context["form"]= form return render(request, 'DailyBudget/subcategory_form.html', context) Error : Exception Value: Field 'id' expected a number but got . -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/d1fb0a49-c582-4623-b391-9f1e54e57c3f%40googlegroups.com.