tags:

views:

291

answers:

2

Hi,

My forms' are never fully instantiated, particularly ImageFields (as how they would be like in admin), and TextFields (which appear as tags)

However, all other fields are instantiated properly.

May I know why this happens?

Attached code that replicates this issue:

#view

sellerForm = SellerUpgradeForm(instance=userseller_prof)

#form

class SellerUpgradeForm(ModelForm):
    def __init__(self,*args, **kwargs):
        super(SellerUpgradeForm, self).__init__(*args, **kwargs)
        self.fields['selling_currency'].queryset = Countries.objects.filter(paypal_valid="yes")
    class Meta:
        model = UserSeller
        fields = ("seller_store_image","shop_description","selling_currency","auth_username",
                  "auth_password","auth_signature")

#model

class UserSeller(models.Model):
    user = models.ForeignKey(User, verbose_name="User")
    access_plan = models.ForeignKey(AccessPlan, verbose_name="Seller's Access Plan")
    status = models.CharField(max_length=100, choices=SELLER_STATUS, verbose_name="Seller Status", default="unapproved")
    rating = models.DecimalField(verbose_name="Rating (Upon 5)", decimal_places=2, max_digits=4, default=0)
    total_rating_count = models.IntegerField(verbose_name="Rating Count", default=0)
    selling_currency = models.ForeignKey(Countries, verbose_name="Currency that seller's product will be sold in")
    meta = models.ManyToManyField(UserMeta, verbose_name="User Meta Data", blank=True, related_name="seller_meta")
    shop_description = models.TextField(verbose_name="Description about the products you are selling")
    seller_store_image = models.ImageField(upload_to=settings.CUSTOM_UPLOAD_DIR, verbose_name="Store Avatar", blank=True)
    internal_note = models.TextField(verbose_name="Seller Note (Internal Use Only)", blank=True)
    timestamp = models.DateTimeField(verbose_name="Date Created", auto_now_add=True)
    auth_username = models.CharField(max_length=50, verbose_name="Paypal Auth Username")
    auth_password = models.CharField(max_length=50, verbose_name="Paypal Auth Password")
    auth_signature = models.CharField(max_length=100, verbose_name="Paypal Auth Signature")
    class Meta:
        verbose_name = "Seller Profile"
        verbose_name_plural = "Seller Profiles"
        app_label = "management"
    def __unicode__(self):
        return "Rating: " + str(self.rating)
A: 

Oops. I had a JS script that auto clears textareas on document.ready hence the following. forgot about this.

My bad.

nubela
A: 

The reason why the ImageFields are not pre-populated is a browser security issue, nothing to do with Django.

If browsers let web pages pre-populate file inputs, it would be very easy for a site to arbitrarily upload content from a user's hard drive without their consent. So in order to stop that happening, file input fields are always rendered as blank.

Daniel Roseman