开发者

How to override save() method of modelform class and added missing information?

开发者 https://www.devze.com 2023-02-01 00:43 出处:网络
I just started to learn Django and I had a question. I\'m trying to automatically add the missing information, when saving form data. I get to change/add the desired \"cleaned_data\" information by o

I just started to learn Django and I had a question.

I'm trying to automatically add the missing information, when saving form data. I get to change/add the desired "cleaned_data" information by overriding save() method of model开发者_运维百科form class, but changes are not recorded in the database. Actually, how to write the modified information? This is code:

def save(self, commit = True, *args, **kwargs):
    temp = ServiceMethods(url = self.cleaned_data.get('url'), wsdl_url = self.cleaned_data.get('wsdl_url'))

    if not temp.get_wsdl_url():                             
        temp.make_wsdl_url()

    if temp.get_wsdl_url():                                 
        temp.make_wsdl()                                    
        self.cleaned_data['wsdl_url'] = temp.get_wsdl_url() 
        self.cleaned_data['wsdl_description'] = temp.get_wsdl_description()

    super(ServiceForm, self).save(commit = commit, *args, **kwargs)  

And model:

class Services(models.Model):
    name                = models.CharField('Имя', max_length=256)
    url                 = models.URLField('Ссылка', unique = True)
    wsdl_url            = models.URLField('Ссылка на WSDL-документ', blank=True)
    description         = models.TextField('Описание сервиса',blank=True)
    wsdl_description    = models.TextField('WSDL описание', blank=True, editable=False)
    added               = models.DateTimeField('Добавлено', auto_now_add=True)

TIA


Try setting the data on self.instance instead of in self.cleaned_data, and let me know if that works.

0

精彩评论

暂无评论...
验证码 换一张
取 消