我正在構建一個應用程序,企業可以使用一堆信息上傳他們的本地商店。目前,有一種表單正在工作,但我希望實施UpdateView以便企業隨時更新其信息。如何實現基於UpdateView類的視圖
這裏是我的model.py:
class Business(models.Model):
owner = models.OneToOneField(User, null=False)
name = models.CharField(max_length=75)
logo = models.ImageField(null=True, blank=False, upload_to=upload_location)
players = models.CharField(max_length=4, choices=PLAYERS_CHOICES, default='2-4')
average_price = models.IntegerField()
number_games = models.IntegerField()
city = models.CharField(max_length=25, choices=CITY_CHOICES)
address = models.CharField(max_length=200)
website = models.CharField(max_length=300)
description = models.TextField(max_length=1000, null=True)
timestamp = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
slug = models.SlugField(null=True, blank=True)
請注意,我使用的是OneToOneField,因爲一個業主只能在站點內有一個業務。
我對形式views.py:
class BusinessCreateView(LoginRequiredMixin, CreateView):
form_class = BusinessCreateForm
login_url = '/login/'
template_name = 'business/form.html'
success_url = '/'
def form_valid(self, form):
instance = form.save(commit=False)
instance.owner = self.request.user
return super(BusinessCreateView, self).form_valid(form)
這裏是我的forms.py:
class BusinessCreateForm(forms.ModelForm):
class Meta:
model = Business
fields = [
'name',
'logo',
'players',
'average_price',
'number_games',
'city',
'address',
'website',
'description',
]
我已經檢查的文件,我一直沒能實現一個UpdateView。
我該怎麼辦?
非常感謝你提前!
UPDATE
我已經盡我views.py內查詢集並沒有奏效:
def get_queryset(self):
return Business.objects.filter(user=self.request.user)
您的問題不清楚。什麼阻止你實現UpdateView? –