Electronics & Programming

develissimo

Open Source electronics development and programming

  • You are not logged in.

#1 Dec. 27, 2010 04:38:34

B.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

inline-model-formset help


I have a simple model:

class LineItem(models.Model):
customer = models.ForeignKey(Customer)
merchant = models.ForeignKey(Merchant)
product = models.ForeignKey(Product)
child = models.ForeignKey(Pupil)
amount = models.FloatField(blank=False,editable=False,
default=1.000)

In my view I would like the user to create multiple lineitems, where
the customer and the merchant are already defined. The user should
select from a drop down list of products (filtered by merchant), enter
the name of the child and an amount. If the *default* amount is >
1.000 then that is considered the minimum amount for that combination
(merchant+product).

Here is the relevant other model:

class Pupil(models.Model):
parent = models.ForeignKey(Customer,related_name=_(u'Parent'))
school = models.ForeignKey(Merchant,related_name=_(u'School'))
name = models.CharField(blank=False, max_length=100)
relationship =
models.IntegerField(verbose_name=_(u'Relationship'),
choices=(
(1,_(u'Son')),
(2,_(u'Daughter')))
)

I have "manually" created the front-end forms, but I ran into a wall
when trying to redisplay the form with errors. I can display the
errors fine, but I can't figure out how to pre-populate the fields.
After some digging through the docs I came across inline-formsets but
I'm not sure how to wire it up in my scenario.

Thanks for any guidance.

--
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=en.

Offline

#2 Dec. 27, 2010 04:49:00

B.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

inline-model-formset help


I forgot to add that this is the model that defines the relationship between
a merchant and a product:

class PriceList(models.Model):
product = models.ForeignKey(Product)
merchant = models.ForeignKey(Merchant)
minimum_price = models.FloatField(verbose_name=_(u'Minimum Price'),
help_text=_(u'This is the minimum
price that the merchant will accept for this product.\
The default is 1.000. Replace this if
the merchant requires a higher minimum price for\
this product.'),
default=1.000,
blank=False,
null=False)
name = models.CharField(max_length=100,verbose_name=_(u'Custom
Name'),blank=True,null=False,
help_text=_(u'This is a custom name for this
product and merchant. Use this field if\
the merchant requests another name for the
same product, instead of adding new (duplicate) products.'))

I'm not sure how to tie in these models so I can get one form, with the
following fields, which the user can dynamically duplicate (to add more
items).

Product (select drop down, filtered from PriceList for a Merchant)
Pupil Name field
Pupil Relationship field (drop down, Son / Daughter)
Amount (from the LineItem model)

The validation is that the amount entered, should not be less than the
minimum amount specified in the PriceList model for the merchant+product
combination. The user can only select products from one merchant (ie, one
PriceList) at one time; to select from another merchant they have to start
the workflow again - by selecting a new merchant, from a different view.

Hope this helps clarify my problem.

--
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=en.

Offline

#3 Dec. 27, 2010 07:54:43

Emmanuel M.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

inline-model-formset help


Look at modelforms with instance...http://docs.djangoproject.com/en/dev/topics/forms/modelforms/--
Emmanuel Mayssat


On Sun, Dec 26, 2010 at 8:48 PM, Burhan <burhan.kha...@gmail.com> wrote:
> I forgot to add that this is the model that defines the relationship between
> a merchant and a product:
> class PriceList(models.Model):
>     product = models.ForeignKey(Product)
>     merchant = models.ForeignKey(Merchant)
>     minimum_price = models.FloatField(verbose_name=_(u'Minimum Price'),
>                                       help_text=_(u'This is the minimum
> price that the merchant will accept for this product.\
>                                       The default is 1.000. Replace this if
> the merchant requires a higher minimum price for\
>                                       this product.'),
>                                       default=1.000,
>                                       blank=False,
>                                       null=False)
>     name = models.CharField(max_length=100,verbose_name=_(u'Custom
> Name'),blank=True,null=False,
>                                help_text=_(u'This is a custom name for this
> product and merchant. Use this field if\
>                                the merchant requests another name for the
> same product, instead of adding new (duplicate) products.'))
> I'm not sure how to tie in these models so I can get one form, with the
> following fields, which the user can dynamically duplicate (to add more
> items).
> Product (select drop down, filtered from PriceList for a Merchant)
> Pupil Name field
> Pupil Relationship field (drop down, Son / Daughter)
> Amount (from the LineItem model)
> The validation is that the amount entered, should not be less than the
> minimum amount specified in the PriceList model for the merchant+product
> combination. The user can only select products from one merchant (ie, one
> PriceList) at one time; to select from another merchant they have to start
> the workflow again - by selecting a new merchant, from a different view.
> Hope this helps clarify my problem.
>
> --
> 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=en.
>

--
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=en.

Offline

#4 Dec. 27, 2010 08:06:42

B.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

inline-model-formset help


Thanks, but I'm not sure which models to pass to it, when it asks for two
models in this:

"

If you want to create a formset that allows you to edit books belonging to a
particular author, you could do this:

>>> from django.forms.models import inlineformset_factory>>> BookFormSet =
>>> inlineformset_factory(Author, Book)>>> author =
>>> Author.objects.get(name=u'Mike Royko')>>> formset =
>>> BookFormSet(instance=author)

"
So in my case, I have LineItem, which is what I need the user to fill in. So
that would the inlineform, since I need more than one of those in my view,
but what do I use for formset?

In this line: inlineformset_factory(Author, Book)

Not sure how to translate that to my models.

Thanks again.

--
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=en.

Offline

#5 Dec. 27, 2010 08:43:55

Emmanuel M.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

inline-model-formset help


I don't think you want a form set, but rather a modelform.
Look at the top (not the bottom!) of the pagehttp://docs.djangoproject.com/en/dev/topics/forms/modelforms/On Mon, Dec 27, 2010 at 12:06 AM, Burhan <burhan.kha...@gmail.com> wrote:
> Thanks, but I'm not sure which models to pass to it, when it asks for two
> models in this:
> "
>
> If you want to create a formset that allows you to edit books belonging to a
> particular author, you could do this:
>
>>>> from django.forms.models import inlineformset_factory
>>>> BookFormSet = inlineformset_factory(Author, Book)
>>>> author = Author.objects.get(name=u'Mike Royko')
>>>> formset = BookFormSet(instance=author)
>
> "
> So in my case, I have LineItem, which is what I need the user to fill in. So
> that would the inlineform, since I need more than one of those in my view,
> but what do I use for formset?
> In this line: inlineformset_factory(Author, Book)
> Not sure how to translate that to my models.
> Thanks again.
>
> --
> 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=en.
>

--
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=en.

Offline

#6 Dec. 27, 2010 16:56:22

B.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

inline-model-formset help


Thanks for that, I managed to get my form working the way I want. Now the
only problem is I need to duplicate this form, well, actually, I need the
user to be able to add multiple "items" in one view.

Right now my form is "ModelForm", so to get multiple model forms in one view
(and get the management form), do I need to use formset?

--
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=en.

Offline

Board footer

Moderator control

Enjoy the 21st of August
PoweredBy

The Forums are managed by develissimo stuff members, if you find any issues or misplaced content please help us to fix it. Thank you! Tell us via Contact Options
Leave a Message
Welcome to Develissimo Live Support