在UpdateView之后默认更改值

时间:2019-03-24 14:22:13

标签: python django django-views

首先,我对英语中的错误表示歉意。
我有三种模型:书籍(通用模型),书籍信息(关于书籍的信息),具有外键的位置(在图书馆中的位置)。

models.py

class Location(models.Model):
    room = models.PositiveIntegerField()
    bookcase = models.PositiveIntegerField()
    shelf = models.PositiveIntegerField()


class BookInfo(models.Model):
    title = models.CharField(max_length=120)
    author = models.CharField(max_length=150)
    published = models.CharField(max_length=4)
    position = models.ForeignKey(Location, on_delete = models.CASCADE, related_name = 'position_information')

    def __str__(self):
        return self.title
    class Meta:
        ordering = ['-id']
        unique_together = ['title', 'author']


class Books(models.Model):

    ON_HANDS = 1
    IN_LIBRARY = 0
    ALARM = -1

    STATUS = (
        (ON_HANDS, 'The book is on hands'),
        (IN_LIBRARY, 'The book is in library'),
        (ALARM, 'Need to return the book')
    )

    book = models.ForeignKey('BookInfo', on_delete = models.CASCADE, related_name = 'book_information')
    date_of_issue = models.DateField(blank= True, default=timezone.now)
    date_of_return = models.DateField(blank= True, default=date.today() + timedelta(days=14))
    status_of_book = models.IntegerField(choices = STATUS, default = 0)

    def __str__(self):
        return self.book.title

    class Meta:
        ordering = ['-id']

forms.py

    class BookFormUpdate(forms.ModelForm):
        class Meta:
            model = Books

            fields = ['book','date_of_issue','date_of_return','status_of_book']
            widgets = {
                'date_of_issue': forms.DateInput(format=('%Y-%m-%d'), attrs={'class':'form-control', 'placeholder':'Select a date', 'type':'date'}),
            'date_of_return': forms.DateInput(format=('%Y-%m-%d'), attrs={'class':'form-control', 'placeholder':'Select a date', 'type':'date'})
            }

当我使用“书籍更新表单”并将书籍状态更改为1或-1时,我想将书籍的位置自动更改为(0,0,0)。我覆盖了UpdateView,但是遇到了一些问题。

views.py

class MainUpdate(UpdateView):  
    model = Books
    template_name = 'core/detail.html'
    form = BookFormUpdate

    def form_valid(self, form):
        form.save()
        categories = get_object_or_404(self.model, pk=self.kwargs.get('pk'))
        stat = form.cleaned_data['status_of_book']
        if stat == 1 or stat == -1:
            book_id = categories.book.id
            BookInfo.objects.filter(id=book_id).update(position=Location.objects.get(id=32))
        return redirect('/books-info/')

为了更新位置,我使用了 Location.objects.get(id = 32)(id = 32对应于位置(0,0,0))。 是否有另一种解决方案或更好的方法而不覆盖UpdateView以自动更改位置?

我正在尝试使用这样的代码:

# Not work
BookInfo.objects.filter(id=book_id).update(position__room=0,position__bookcase=0,position__shelf=0)

# Not work
BookInfo.objects.select_related().filter(id=book_id).update(position__room=0,position__bookcase=0,position__shelf=0)

# Not work
test = BookInfo.objects.get(id=book_id)
test.position__room=0
test.position__bookcase=0
test.position__shelf=0
test.save(update_fields=['position__room','position__bookcase','position__shelf'])

感谢您的建议和帮助

0 个答案:

没有答案