from django.db import models
from ckeditor.fields import RichTextField
from ckeditor_uploader.fields import RichTextUploadingField
from django.db.models.signals import post_save
from django.dispatch import receiver
import uuid
# Create your models here.
import random
class Themes(models.Model):
theme = models.CharField(max_length=100)
def __str__(self):
return self.theme
class Meta:
verbose_name = "Mavzu"
verbose_name_plural = "Mavzular"
ordering=['id']
class Question(models.Model):
theme = models.ForeignKey(Themes,verbose_name="Mavzusi:", on_delete=models.CASCADE)
question = RichTextUploadingField("Savol",config_name='default2')
def __str__(self):
return self.question
class Meta:
verbose_name = "Savol"
verbose_name_plural = "Savollar"
order_with_respect_to = 'theme'
class Answer(models.Model):
question = models.ForeignKey(Question, on_delete=models.CASCADE)
text = RichTextUploadingField("Javob:",config_name='ans')
is_correct = models.BooleanField("To'g'ri javob:", default=False)
def __str__(self):
return self.text
class Meta:
verbose_name = "Javob"
verbose_name_plural = "Javoblar"
order_with_respect_to = 'question'
class Option(models.Model):
themes = models.ManyToManyField(Themes,verbose_name="Mavzular")
qbytheme=models.CharField(max_length=100,verbose_name="Savollarning qaysi mavzudan qanchaligi tartib bo'yicha")
questions = models.ManyToManyField(Question,verbose_name="Savollar",blank=True)
def __str__(self):
return str(self.id)
# # self.questions.clear()
# cn = list(map(int,self.qbytheme.split(',')))
# for i in range(len(cn)):
# cur = self.themes.all()[i]
# ques=cur.question_set.all()
# if list(ques)!=[]:
# ran=random.choices(ques,k=cn[i])
# self.questions.add(*ran)
# print(self.questions.all())
@receiver(post_save,sender=Option,dispatch_uid="option_post_save")
def update_options(sender,instance,**kwargs):
c=Question.objects.first()
instance.questions.add(c)# problem here
instance.questions.update()
print(instance.questions.all())