There's probably a very simple answer to this but I can't see it at
the moment:

This model:

class Product(models.Model):
        user = models.ForeignKey(User, related_name="product_list",
editable=False)
        productid = models.CharField(max_length=40, blank=True)
        prodname = models.CharField(max_length=255, verbose_name='Product
name')
        proddesc = models.TextField(blank=True, verbose_name='Description')
        prodtopcat = models.ForeignKey(ProductTopCategory,
verbose_name='Product Main Category')
        prodsubcat = models.ForeignKey(ProductSubCategory,
verbose_name='Product Sub Category')
        unit = models.ForeignKey(Units, verbose_name='Unit')
        video = models.ManyToManyField('Video', blank=True,
verbose_name='Video')
        costperunit = models.DecimalField(max_digits=6, decimal_places=2,
verbose_name='Cost per unit')
        costperlot = models.DecimalField(max_digits=6, decimal_places=2,
verbose_name='Cost per lot')
        available = models.BooleanField(default=True)
        featured = models.BooleanField(blank=True, null=True)

is used to generate a ModelForm with two modified fields that enable
the field 'prodsubcat' to be dynamically populated based on the
selection of 'prodtopcat'.

class DynamicChoiceField(forms.ChoiceField):
        def clean(self, value):
                return value

class ProductForm(ModelForm):
        prodsubcat = DynamicChoiceField(widget=forms.Select(attrs=
{'disabled': 'true'}), choices =(('-1', 'Select Top Category'),))

I took this from an item I found on the web. I think setting the
'disabled' attribute is all I actually need.

The dynamic lookup is performed using jQuery.getJSON with this view:

def feeds_subcat(request, cat_id):
        from django.core import serializers
        json_subcat = serializers.serialize("json",
ProductSubCategory.objects.filter(prodcat = cat_id), fields=('id',
'shortname', 'longname'))
        return HttpResponse(json_subcat, mimetype="application/javascript")

and the SELECT HTML is generated like this:

options += '<option value="' + j[i].pk + '">' + j[i].fields
['longname'] + '</option>';

This all works, but on submitting the form, I get the error 'Cannot
assign "u'17'": "Product.prodsubcat" must be a "ProductSubCategory"
instance' where "u'17'" is the option value of id_prodsubcat

What type of variable should I be passing (I assume int) and where
should I be converting it? In validation, in the pre-save signal, in
the save signal? Or should I be trying to convert it on the form
itself? Where is the ForeignKey instance save process documented?

TIA
Simon
--~--~---------~--~----~------------~-------~--~----~
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