Эксперимент 1
Использовать дочернюю таблицу для получения записей из первичных
This commit is contained in:
1 parent
785f9f0ba8
commit
c95853db46
5 files changed
+37
-27
No files matched your search
BIN
Binary file not shown.
+6
-4
@@ -12,7 +12,8 @@ class BaseCriteria(models.Model):
|
||||
verbose_name_plural = "Названия"
|
||||
def __str__(self):
|
||||
return str(self.name)
|
||||
|
||||
|
||||
# Первичная модель
|
||||
class UserComment(models.Model):
|
||||
user = models.ForeignKey(settings.AUTH_USER_MODEL, editable=False, on_delete=models.PROTECT)
|
||||
time_create = models.DateTimeField(auto_now_add = True)
|
||||
@@ -24,11 +25,12 @@ class UserComment(models.Model):
|
||||
|
||||
def __str__(self):
|
||||
return str(self.comment)
|
||||
|
||||
|
||||
# Дочерняя модель
|
||||
class RatingStatus(models.Model):
|
||||
crit = models.ForeignKey(BaseCriteria, on_delete=models.PROTECT)
|
||||
crit = models.ForeignKey(BaseCriteria, on_delete=models.PROTECT, related_name='get_criteria')
|
||||
rating = models.FloatField()
|
||||
usercom = models.ForeignKey(UserComment, on_delete=models.PROTECT)
|
||||
usercom = models.ForeignKey(UserComment, on_delete=models.PROTECT, related_name='get_rating')
|
||||
|
||||
class Meta:
|
||||
verbose_name = "Рейтинг"
|
||||
|
||||
+14
-10
@@ -2,23 +2,27 @@ from rest_framework import serializers
|
||||
from .models import *
|
||||
|
||||
|
||||
|
||||
class RatingStatusSerializer(serializers.ModelSerializer):
|
||||
depth = 1
|
||||
|
||||
class Meta:
|
||||
model = RatingStatus
|
||||
fields = '__all__'
|
||||
|
||||
class BaseCriteriaSerializer(serializers.ModelSerializer):
|
||||
|
||||
get_criteria = RatingStatusSerializer(many=True)
|
||||
|
||||
class Meta:
|
||||
model = BaseCriteria
|
||||
fields = '__all__'
|
||||
|
||||
class UserCommentSerializer(serializers.ModelSerializer):
|
||||
|
||||
class UserCommentSerializer(serializers.ModelSerializer):
|
||||
|
||||
get_rating = RatingStatusSerializer(many=True)
|
||||
|
||||
class Meta:
|
||||
model = UserComment
|
||||
fields = '__all__'
|
||||
|
||||
class RatingStatusSerializer(serializers.ModelSerializer):
|
||||
|
||||
# crit =serializers.SlugRelatedField(slug_field="name", read_only=True)
|
||||
|
||||
class Meta:
|
||||
model = RatingStatus
|
||||
fields = '__all__'
|
||||
fields = '__all__'
|
||||
@@ -19,6 +19,9 @@ class UserCommentAPI(ListCreateAPIView):
|
||||
|
||||
def get_queryset(self):
|
||||
queryset = UserComment.objects.filter(user_id=self.request.user.id, time_create__contains=datetime.date.today())
|
||||
queryset = queryset.prefetch_related('get_rating')
|
||||
|
||||
|
||||
return queryset
|
||||
|
||||
def perform_create(self, serializer):
|
||||
|
||||
@@ -104,7 +104,7 @@ export default {
|
||||
.then(data => {
|
||||
t_baseCriteria.value = data.data.results
|
||||
})
|
||||
console.log(t_baseCriteria.value, 't_baseCriteria.value');
|
||||
|
||||
}
|
||||
|
||||
function f_userComment() {
|
||||
@@ -112,14 +112,14 @@ export default {
|
||||
.then(data => {
|
||||
t_userComment.value = data.data.results
|
||||
})
|
||||
console.log(t_userComment.value, 't_userComment.value');
|
||||
|
||||
}
|
||||
|
||||
onMounted(() => {
|
||||
|
||||
f_baseCriteria()
|
||||
f_userComment()
|
||||
|
||||
|
||||
})
|
||||
|
||||
function feedAll() {
|
||||
@@ -173,17 +173,18 @@ export default {
|
||||
}
|
||||
|
||||
function proba() {
|
||||
console.log(ratingStatus.value.decFeed, 'ratingStatus.value.decFeed');
|
||||
const feedObj = {}
|
||||
console.log(t_userComment.value, 't_userComment.value');
|
||||
// console.log(ratingStatus.value.decFeed, 'ratingStatus.value.decFeed');
|
||||
// const feedObj = {}
|
||||
|
||||
feedObj.comment = comment.value
|
||||
feedObj.ratingstatus = ratingStatus.value.decFeed
|
||||
if (t_userComment.value.length==0) {
|
||||
axiosApiInstance.post(`api/feedback/userComment/`, feedObj)
|
||||
}
|
||||
else {
|
||||
axiosApiInstance.put(`api/feedback/userCommentUp/${t_userComment.value[0].id}/`, feedObj)
|
||||
}
|
||||
// feedObj.comment = comment.value
|
||||
// feedObj.ratingstatus = ratingStatus.value.decFeed
|
||||
// if (t_userComment.value.length==0) {
|
||||
// axiosApiInstance.post(`api/feedback/userComment/`, feedObj)
|
||||
// }
|
||||
// else {
|
||||
// axiosApiInstance.put(`api/feedback/userCommentUp/${t_userComment.value[0].id}/`, feedObj)
|
||||
// }
|
||||
}
|
||||
|
||||
return {t_baseCriteria, tab_userComment, comment, average, feedAll, feedUp, menusCloses, myLabel, proba}
|
||||
|
||||
Reference in new issue
Block a user