Opened 14 years ago

Closed 14 years ago

Last modified 14 years ago

#14452 closed (invalid)

Need help understanding why the below is happening.

Reported by: danols@… Owned by: nobody
Component: Forms Version: 1.2
Severity: Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

I've attempted to get help in the #django IRC but with no luck. I hope you can assist.

"""
How is this possible ??? 

Every time I refresh the page more 'same' choices get added to the fields choice options. That is:

First time I will get:
a
b

Second refresh
a
b
a
b

and so on.... ??? I don't get how this is happening. 
"""

### in view 
    form = AdForm(instance=ad)
    AdTargetFormSet = modelformset_factory(AdTarget, form=AdTargetForm, can_delete=True)
    AdTargetFormSet.form = staticmethod(curry(AdTargetForm, objects=[Competition.objects.all()]))
    adtarget_formset = AdTargetFormSet(queryset=AdTarget.objects.filter(ad=ad))
   
### my form
class AdTargetForm(ModelForm):
    '''
    Creates and AdTargetForm
    
    Takes an argument that is a list of objects
    to generate the url pattern - the object
    must have a valid get_absolute_url defined.
    
    for example AdTargetForm(objects=[Competition.objects.all(), Team.objects.all()]
    '''
    # create the ad tragets lists
    AD_TARGETS = [
              ('', '...'),
              ('.*', 'The Entire Site'),
              ('^/$', 'Home Landing Page'),
              ]
    
    def __init__(self, objects=None, *args, **kwargs):
        super(AdTargetForm, self).__init__(*args, **kwargs)
        
        if (objects):
            for object_list in objects:
                # @TODO: add the object landing page
                for obj in object_list:
                    self.AD_TARGETS.append(('^%s$' % obj.get_absolute_url(), str(obj)))
            self.fields['url_pattern'].choices = self.AD_TARGETS

    url_pattern = forms.ChoiceField(label='Target', choices = AD_TARGETS)
    class Meta:
        model = AdTarget
        fields = () # show these fields from model
        exclude = ('ad') # exclude these fields from model

Change History (3)

comment:1 by Matt McClanahan, 14 years ago

Resolution: invalid
Status: newclosed

Hello, Trac is designed for filing bugs about Django itself, not for questions on how to use Django. For those you can use either the django-users mailing list, or the #django channel on irc.freenode.net.

comment:2 by Luke Plant, 14 years ago

mattmcc is exactly right, but here is the answer to your question, since you did actually ask on IRC: the reason your list keeps growing is because you keep appending to it. AD_TARGETS is a class attribute i.e. it is attached to the class AdTargetForm, not to instances of the class. Since the class AdTargetForm is a global object, so is AD_TARGETS. The class AdTargetForm is created when you first access that module, and it is not destroyed until you stop the server. So, every time you append to that variable, it keeps getting longer.

comment:3 by danols@…, 14 years ago

Thank you for the clarification - it is so obvious now and I got it to work. I will use the django-users mailing list in the future.

Note: See TracTickets for help on using tickets.
Back to Top