我有这个问题捕捉自拍模型的PK,我正在尝试保存评论,但我有这个错误,错误是在views.py的CreateComment类中,请大家我需要一只手< / p> models.py
中的
模板 回溯
在views.py中SELECT T1.RepID, T1.Role, Min(T1.StartDate) AS StartDate, Max(T1.EndDate) AS EndDate
FROM
(SELECT rD1.RepID, rD1.Role, rD1.StartDate, rD1.EndDate
FROM repDetails rD1
INNER JOIN repDetails rD2
ON rD2.RepID = rD1.RepID AND rD2.StartDate = DateAdd (Day, 1, rD1.EndDate) AND (rD2.Role = rD1.Role OR (rD2.Role IS NULL AND rD1.Role IS NULL) OR (rD2.Role = '' AND rD1.Role = ''))
UNION
SELECT rD2.RepID, rD2.Role, rD2.StartDate, rD2.EndDate
FROM repDetails rD1
INNER JOIN repDetails rD2
ON rD2.RepID = rD1.RepID AND rD2.StartDate = DateAdd (Day, 1, rD1.EndDate) AND (rD2.Role = rD1.Role OR (rD2.Role IS NULL AND rD1.Role IS NULL) OR (rD2.Role = '' AND rD1.Role = ''))
) T1
GROUP BY T1.RepID, T1.Role
UNION
SELECT EP.RepID, EP.Role AS DataValue, EP.StartDate, EP.EndDate
FROM repDetails EP
LEFT OUTER JOIN
(SELECT rD1.RepID, rD1.Role, rD1.StartDate, rD1.EndDate
FROM repDetails rD1
INNER JOIN repDetails rD2
ON rD2.RepID = rD1.RepID AND rD2.StartDate = DateAdd (Day, 1, rD1.EndDate) AND (rD2.Role = rD1.Role OR (rD2.Role IS NULL AND rD1.Role IS NULL) OR (rD2.Role = '' AND rD1.Role = ''))
UNION
SELECT rD2.RepID, rD2.Role , rD2.StartDate, rD2.EndDate
FROM repDetails rD1
INNER JOIN repDetails rD2
ON rD2.RepID = rD1.RepID AND rD2.StartDate = DateAdd (Day, 1, rD1.EndDate) AND (rD2.Role = rD1.Role OR (rD2.Role IS NULL AND rD1.Role IS NULL) OR (rD2.Role = '' AND rD1.Role = ''))
) T1
ON EP.RepID = T1.RepID AND EP.StartDate = T1.StartDate
WHERE T1.RepID IS NULL
class Selfie(models.Model):
selfie_title = models.CharField(max_length=50)
selfie_description = models.TextField(null=True, blank=True)
selfie_image = models.ImageField(upload_to="images/",
width_field="selfie_width",
height_field="selfie_height",
)
selfie_height = models.IntegerField(default=0)
selfie_width = models.IntegerField(default=0)
selfie_favorite = models.BooleanField()
selfie_user = models.ForeignKey("auth.User")
selfie_timestamp = models.DateTimeField(auto_now=True)
def __unicode__(self):
return unicode(self.selfie_title)
def __str__(self):
return self.selfie_title
class Comment(models.Model):
comment_user = models.ForeignKey("auth.User")
body_comment = models.TextField()
timestamp_comment = models.DateTimeField(auto_now=True)
post_comment = models.ForeignKey("Selfie")
def __unicode__(self):
return unicode(self.body_comment[:20])
def __str__(self):
return self.body_comment[:20]
class SelfieDetail(DetailView):
template_name = "SelfieDetail.html"
model = Selfie
def get_context_data(self, **kwargs):
context = super(SelfieDetail, self).get_context_data(**kwargs)
context['form'] = CommentForm
if self.request.POST:
context['form'] = CommentForm(self.request.POST)
else:
context['form'] = CommentForm()
return context
class CreateComment(FormView):
form_class = CommentForm
success_url = '../login'
template_name = "SelfieDetail.html"
def get_form_kwargs(self):
form_kwargs = super(CreateComment, self).get_form_kwargs()
if 'pk' in self.kwargs:
form_kwargs['instance'] = models.Selfie.objects.get(pk=int(self.kwargs['pk']))
return form_kwargs
def form_valid(self, form, *args, **kwargs):
context = self.get_context_data()
form = context['form']
obj = form.save(commit=False)
obj.comment_user_id = self.request.user
#obj.post_comment_id = kwargs={'pk': Selfie.pk} .... self.request.POST.get('comment')
obj.post_comment_id = self.request.GET.get( 'post_comment_id' )
obj.save()
return redirect('/login')
<form method="POST" action="{% url 'createComment' %}">
{% csrf_token %}
{{ form.as_p }}
<button type="submit" class="btn btn-lg">(Comentar)</button>
</form>
答案 0 :(得分:0)
您无法将user
分配给comment_user_id
,因为它需要一个整数。以下任何一种都可以使用:
obj.comment_user_id = self.request.user.id
obj.comment_user = self.request.user