简体   繁体   中英

error django 'dict' object is not callable

I now get the error below because of this line setattr(CsvModel, field.value(), CharField())

Error: 'dict' object is not callable

views.py

if request.method == 'POST':
        form = ConfiguratorForm(data=request.POST)
        # Send import to task.
        # Clean all data and add to var data.
        if form.is_valid():
            data = form.cleaned_data
            process_upload.delay(upload_id=upload.id, form=data)

tasks.py

@task
def process_upload(upload_id, form):
    upload = Upload.objects.get(id=upload_id)
    upload.process(form=form)

process

import_this(data=self.filepath, extra_fields=[
                {'value': self.group_id, 'position': 5},
                {'value': self.uploaded_by.id, 'position': 6}], form=form)

model.py

def import_this(form, *args, **kw):
    # make custom ContactCSVModel
    class ContactCSVModel(CsvModel):

        for k, v in form:
            setattr(CsvModel, v, CharField())


        group = DjangoModelField(Group)
        contact_owner = DjangoModelField(User)

        class Meta:
            delimiter = ","
            dbModel = Contact
            update = {'keys': ["mobile", "group"]}

    return ContactCSVModel.import_data(*args, **kw)

In the second call you are passing in form.cleaned_data , which is a mapping (a dict ), so you are looping over the keys, which are strings.

In the first call on the other hand, you are passing in the form itself. The second call is thus not the same; the following call would be:

form = ConfiguratorForm(data=request.POST)
if form.is_valid():
    process_upload(upload_id=upload.id, form=form)

The question here is if that is what you actually meant to do.

In the first case your form argument is a form instance.

In the second your form argument is a dict instance.

Additionnally:

I think your import_this method (if it is, indeed, a method of your model) lacks a self first argument or should be declared as a @staticmethod . It may save you the trouble of calling it with import_this(self.xxx, self.yyy, ...) But I'm not quite sure how you go from upload.process() to a call to import_method() (my Django is a bit rusty).

And for what it's worth I think there are much simpler ways to achieve what you want to achieve (which if I guess well, is load CSV file):

Without form validation: have a look at csv.DictReader () will return a dict for each line in your file. Then do:

with csv.DictReader(...) as r:
    for line in r:
        instance = MyModel(**line)
        instance.save()

With a form to validate data read from the file: do the same but instanciate a ModelForm , which you'll need to define:

 with csv.DictReader(...) as r:
     for line in r:
         form = MyModelForm(**line)
         form.save()

What you are doing looks like some kind of strange, brain-damaging, meta-programming... Are you trying to create a model on the fly, from what you find in a CSV file ?

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM