I have the following constellation of Tables, which are linked via FK's:
(omitted a few fields for better readability)
class ProductDetail(models.Model):
product = models.ForeignKey(Product, on_delete=models.CASCADE)
product_detail = models.CharField(max_length=100, primary_key=True, verbose_name='Product Detail Name')
materialcode = models.CharField(max_length=20, blank=False, null=False, verbose_name='Material-Code')
billing_model = models.ForeignKey(BillingModel, on_delete=models.CASCADE)
....
--------------
class MinimumRevenue(models.Model):
operator = models.ForeignKey(Operator, on_delete=models.CASCADE)
billing_model = models.ForeignKey(BillingModel, on_delete=models.CASCADE)
minimum_revenue = models.DecimalField(decimal_places=2, max_digits=20, verbose_name='Minimum Revenue')
currency = models.ForeignKey(Currency, on_delete=models.CASCADE)
product_detail = models.ForeignKey(ProductDetail, on_delete=models.CASCADE, verbose_name='Product Detail')
event_type_assignment = models.CharField(max_length=100, verbose_name='Event Type Assignment')
start_date = models.DateField(blank=False, null=False, verbose_name='Start Date', default=get_first_of_month)
end_date = models.DateField(blank=False, null=False, verbose_name='End Date')
....
And a Table (Django_Tables2) which points to the MinimumRevenue Model (which works as designed), now I would like to also show some more related fields in my Table:
class MinimumRevenueTable(tables.Table):
edit = tables.LinkColumn('non_voice:minimum_revenue_update', orderable=False, text='Edit', args=[A('pk')])
invoice_text = tables.TemplateColumn(
'<data-toggle="tooltip" title="{{record.context}}">{{record.invoice_text|truncatewords:2}}')
start_date = tables.DateColumn(format='Y-m-d')
end_date = tables.DateColumn(format='Y-m-d')
foreigncolumn = tables.Column(accessor='productdetail.materialcode')
class Meta:
model = MinimumRevenue
template_name = "django_tables2/bootstrap4.html"
fields = ('operator', 'billing_model', 'minimum_revenue', 'product', 'product_detail', 'event_type_assignment',
'start_date', 'end_date', 'invoice_text', 'currency', 'foreigncolumn')
attrs = {'class': 'table table-hover', }
The foreigncolumn column is never filled, just showing '-', I also tried it with other columns of ProductDetail, but never get any result, would really appreciate any solutions!
Related
I have models named Class, StudentList, Child, TakingQuiz and TakingQuizAnswer. Students can take exams. In this case, when they start the exam, data is added to the 'TakingQuiz' table. With each new answer, the answers are also recorded in the TakingQuizAnswer table.
The result I want to reach -> The question with the most mistakes in the exams solved by the students in a class.
I tried to use Count for this. I'm filtering answer_is_correct to False but that is insufficient. I also need to filter this data for the question column. So I need to get rows where both question and answer_is_correct = False columns are the same and return the first few most repetitive data as results.
I always get a general result in my experiments. I can't include rows where the question column is the same. How can I access the questions with the most mistakes in exams solved by students studying in a class?
Serializer
class ClassSerializerReport(ModelSerializer):
instructor = InstructorSerializerReport(source="instructor.user")
students = StudenListSerializerReport(many=True,
source="student_list_class")
max_incorrect_question = serializers.SerializerMethodField()
class Meta:
model = Class
exclude = [
"created_at",
"updated_at",
"school",
]
def get_max_incorrect_question(self, obj):
data = Class.objects.filter(id = obj.id).values('student_list_class__child__child_taking_quiz__taking_quizes').annotate(res = Count('student_list_class__child__child_taking_quiz__taking_quizes__question', filter = Q(student_list_class__child__child_taking_quiz__taking_quizes__answer_is_correct = False)))
print(data)
return {"question_id" : "I couldn't access that result yet."}
Related Models
class Class(AbstractSchoolBaseModel):
school = models.ForeignKey(
"school.School",
on_delete=models.CASCADE,
related_name="classes_school",
)
instructor = models.ForeignKey(
"account.InstructorProfile",
on_delete=models.CASCADE,
related_name="instructors_school",
)
name = models.CharField(
max_length=50,
)
grade = models.IntegerField(
)
class StudentList(AbstractSchoolBaseModel):
school_class = models.ForeignKey(
"school.Class",
on_delete=models.CASCADE,
related_name="student_list_class",
)
child = models.ForeignKey(
"account.ChildProfile",
on_delete=models.CASCADE,
related_name="student_list_children",
)
class ChildProfile(models.Model):
user = models.OneToOneField(
settings.AUTH_USER_MODEL,
on_delete=models.CASCADE,
primary_key=True,
related_name="user_child")
city = models.ForeignKey(
"country.City",
on_delete=models.SET_NULL,
null=True,
blank=True,
related_name="city_child_profiles")
hobbies = models.CharField(
max_length=500,
null=True,
blank=True,
)
class TakingQuiz(AbstractQuizBaseModel):
quiz = models.ForeignKey("quiz.Quiz", on_delete=models.DO_NOTHING, related_name="quiz_taking_quiz")
child = models.ForeignKey("account.ChildProfile", on_delete=models.CASCADE, related_name = "child_taking_quiz")
title = models.CharField(max_length=150, editable=False, default="-")
total_point = models.PositiveIntegerField(default=0)
class TakingQuizAnswer(AbstractQuizBaseModel):
taking_quiz = models.ForeignKey("quiz.TakingQuiz", on_delete=models.CASCADE, related_name="taking_quizes")
question = models.ForeignKey("quiz.Question", on_delete=models.DO_NOTHING, related_name="question_taking_quiz"e)
answer = models.ForeignKey("quiz.Answer", on_delete= models.DO_NOTHING, related_name="answer_taking_quiz")
taking_quiz_title = models.TextField(editable=False, null=True, blank=True)
question_text = models.TextField(editable=False, null=True, blank=True)
question_topic_content = models.TextField(editable=False, null=True, blank=True)
answer_text = models.TextField(editable=False, null=True, blank=True)
answer_is_correct = models.BooleanField(editable=False, null=True, blank=True)
class Quiz(AbstractQuizBaseModel):
ENABLED_CHOICES = (
(True, _("Active")),
(False, _("Not Active")),
)
book = models.ForeignKey("book.Book", on_delete= models.CASCADE, related_name="book_quiz")
title = models.CharField(max_length=150)
enabled = models.BooleanField(choices=ENABLED_CHOICES, default=False)
class Question(AbstractQuizBaseModel):
quiz = models.ForeignKey("quiz.Quiz", on_delete=models.DO_NOTHING, related_name="question_quiz")
question = models.CharField(max_length=500)
topic = models.CharField(max_length=500)
class Answer(AbstractQuizBaseModel):
IS_CORRECT_CHOICES = (
(True, _("Correct Answer")),
(False, _("Wrong Answer"))
)
question = models.ForeignKey("quiz.Question", on_delete=models.CASCADE, related_name = "question_answer")
answer = models.CharField(max_length=500)
is_correct = models.BooleanField(choices=IS_CORRECT_CHOICES, default=False)
I have two Django models related by a ManyToManyField relationship. Everything works fine except for the inline add dropdown which lists ugly automatically created object names instead of allowing me to format it. How can I specify that?
Models:
class Job(models.Model):
type = models.CharField(max_length=32, choices=JobChoices)
guid = models.CharField(max_length=32)
title = models.CharField(max_length=256)
started_time = models.DateTimeField()
ended_time = models.DateTimeField(blank=True, null=True)
enabled = models.BooleanField(default=False)
running = models.BooleanField(default=False)
working_job_status = models.CharField(max_length=32, choices=StatusCoices)
working_job_length = models.IntegerField(blank=True, null=True)
working_job_progress = models.IntegerField(blank=True, null=True)
working_job_eta_sec = models.IntegerField(blank=True, null=True)
RepeatUnit = (
('s', 'Second'),
('m', 'Minute'),
('h', 'Hour'),
('d', 'Day'),
('W', 'Week'),
('M', 'Month'),
('Y', 'Year'),
)
class Schedule(models.Model):
title = models.CharField(max_length=128)
job = models.ManyToManyField(Job, blank=True, null=True)
start_time = models.DateTimeField(null=False)
end_time = models.DateTimeField(blank=True, null=True)
repeat_unit = models.CharField(blank=True, null=True, max_length=1, choices=RepeatUnit)
repeat_every = models.IntegerField(blank=True, null=True)
repeat_max_count = models.IntegerField(blank=True, null=True)
def __unicode__(self):
return f'{self.title}'
Admin:
class ScheduleAdmin(admin.ModelAdmin):
list_display = ['id', 'title', 'start_time', 'end_time', 'repeat_unit', 'repeat_every', 'repeat_max_count']
class ScheduleInline(admin.TabularInline):
model = Schedule.job.through
min_num = 0
extra = 0
# fields = ('title', )
verbose_name = "Schedule"
verbose_name_plural = "Schedules"
class JobAdmin(admin.ModelAdmin):
list_display = ['id', 'type', 'guid', 'title', 'started_time', 'ended_time', 'enabled', 'running', 'progress']
inlines = [ScheduleInline,]
admin.site.register(Schedule, ScheduleAdmin)
admin.site.register(Job, JobAdmin)
And, when I click on the inlines drop-down menu I get:
changing from __unicode__(self) to __str__(self) did the trick
I try to serialize query set
def do(self):
reservations = Reservation.objects.all()
serializer = ReservationSerializer(data=reservations, many=True)
if serializer.is_valid():
encoded_data = json.dumps(serializer.data)
r = requests.post('http://httpbin.org/post', headers={'Content-Type': 'application/json'}, data=encoded_data)
print(r.text)
else:
print(serializer.errors)
And I always get error of
{u'non_field_errors': [u'Expected a list of items but got type "QuerySet".']}
I tried to use values() on query set, and then convert to list, but this way I get objects without nested models
model
class Reservation(models.Model):
start = models.DateField(verbose_name='Заезд', auto_now=False, auto_now_add=False, blank=False)
end = models.DateField(verbose_name='Выезд', auto_now=False, auto_now_add=False, blank=False)
check_in_time = models.TimeField(verbose_name='Время заезда', blank=False)
check_out_time = models.TimeField(verbose_name='Время выезда', blank=False)
has_refund = models.BooleanField(verbose_name='Возвратная бронь', default=True)
payed = models.BooleanField(verbose_name='Оплачено', default=False)
reserved_days = models.ManyToManyField(Day, blank=False)
additional_services = models.ManyToManyField(AdditionalService)
guest_name = models.CharField(verbose_name='Имя гостя', max_length=200, blank=True)
reservation_number = models.CharField(verbose_name='Номер брони', max_length=200, blank=True)
class AdditionalService(models.Model):
payment_date = models.CharField(verbose_name='Дата оплаты', max_length=200, blank=True)
payment_type = models.CharField(verbose_name='Форма оплаты', max_length=200, blank=False)
service = models.CharField(verbose_name='Услуга', max_length=200, blank=False)
quantity = models.IntegerField(blank=False)
price = models.FloatField(blank=False)
class Day(models.Model):
date = models.DateField(auto_now=False, auto_now_add=False)
price = models.FloatField()
payment_method = models.CharField(max_length = 200, blank=True)
payment_date = models.CharField(max_length=200, blank=True)
room = models.ForeignKey(Room, null=True, blank=True, verbose_name='Номер', on_delete=models.CASCADE)
class Room(models.Model):
name = models.CharField(max_length = 200, null=True)
id = models.AutoField(primary_key=True)
room_id = models.CharField(max_length = 200, null=False)
def __unicode__(self):
return self.name
serializers
class ReservationSerializer(serializers.ModelSerializer):
reserved_days = DaySerializer(many=True)
additional_services = AdditionalServicesSerializer(many=True)
class Meta:
model = Reservation
fields = [
'start',
'end',
'check_in_time',
'check_out_time',
'reserved_days',
'additional_services',
'has_refund',
'payed',
'guest_name',
'reservation_number',
]
class DaySerializer(serializers.ModelSerializer):
room = RoomSerializer()
class Meta:
model = Day
fields = [
'date',
'price',
'payment_method',
'payment_date',
'room',
]
class AdditionalServicesSerializer(serializers.ModelSerializer):
class Meta:
model = AdditionalService
fields = [
'payment_date',
'payment_type',
'service',
'quantity',
'price',
]
class RoomSerializer(serializers.ModelSerializer):
class Meta:
model = Room
fields = [
'room_id',
]
For serialization you don't need to use data keyword, just pass queryset as first positional argument:
serializer = ReservationSerializer(reservations, many=True)
return serializer.data
I have following model
class Day(models.Model):
date = models.DateField(auto_now=False, auto_now_add=False)
price = models.FloatField()
payment_method = models.CharField(max_length = 200, blank=True)
payment_date = models.CharField(max_length=200, blank=True)
room = models.ForeignKey(Room, null=True, blank=True, verbose_name='Номер', on_delete=models.CASCADE)
def __unicode__(self):
return str(self.date)
class Reservation(models.Model):
start = models.DateField(verbose_name='Заезд', auto_now=False, auto_now_add=False, blank=False)
end = models.DateField(verbose_name='Выезд', auto_now=False, auto_now_add=False, blank=False)
check_in_time = models.TimeField(verbose_name='Время заезда', blank=False)
check_out_time = models.TimeField(verbose_name='Время выезда', blank=False)
has_refund = models.BooleanField(verbose_name='Возвратная бронь', default=True)
payed = models.BooleanField(verbose_name='Оплачено', default=False)
reserved_days = models.ManyToManyField(Day, blank=False)
additional_services = models.ManyToManyField(AdditionalService)
guest_name = models.CharField(verbose_name='Имя гостя', max_length=200, blank=True)
reservation_number = models.CharField(verbose_name='Номер брони', max_length=200, blank=True)
What I want is to have ability to edit Day on Reservation page
I try the following as in the django docs
class ReservedDaysInline(admin.TabularInline):
model = Reservation
extra = 1
class ReservationAdmin(admin.ModelAdmin):
inlines = (ReservedDaysInline,)
class DayAdmin(admin.ModelAdmin):
inline = (ReservedDaysInline,)
admin.site.register(Reservation, ReservationAdmin)
admin.site.register(Day, DayAdmin)
But it doesnt work.
What am I doing wrong ?
Try this from doc
class ReservedDaysInline(admin.TabularInline):
model = Reservation.reserved_days.through
extra = 1
class ReservationAdmin(admin.ModelAdmin):
inlines = (ReservedDaysInline,)
exclude = ('reserved_days',)
How to display in OrderAdmin: Basket owner, products, quantity of products?
I try Inline:
admin.py:
class BasketInline(admin.TabularInline):
model = Basket
class OrderAdmin(admin.ModelAdmin):
inlines = [
BasketInline,
]
admin.site.register(Order, OrderAdmin)
but it does not work.
class Basket(models.Model):
owner = models.ForeignKey(User, related_name='user_basket', verbose_name='Owner')
name = models.CharField("Basket_Name", max_length=30)
products = models.ManyToManyField('Product', through='BasketProduct', blank=True, null=True)
class BasketProduct(models.Model):
product = models.ForeignKey('Product')
basket = models.ForeignKey('Basket')
quantity = models.IntegerField()
class Product(models.Model):
name = models.CharField(max_length=50)
slug = models.SlugField()
unit_price = models.DecimalField(max_digits=5, decimal_places=2)
desc = models.TextField()
category = models.ManyToManyField(Category)
class ShippingOptions(models.Model):
name = models.CharField(max_length=50)
price = models.DecimalField(max_digits=5, decimal_places=2)
time = models.CharField(max_length=150)
class Order(models.Model):
bask = models.OneToOneField(Basket)
shipp = models.OneToOneField(ShippingOptions)
Maybe I need a different way. Please any help. Thanks
You need foreignKey on Order in you Basket model:
class Basket(models.Model):
owner = models.ForeignKey(User, related_name='user_basket', verbose_name='Owner')
name = models.CharField("Basket_Name", max_length=30)
products = models.ManyToManyField('Product', through='BasketProduct', blank=True, null=True)
owner = models.ForeignKey(Owner, blank=True, verbose_name='Owner')
or you have error in first model row:
owner = models.ForeignKey(Owner, related_name='user_basket', verbose_name='Owner')