I have django model
class TestResult(models.Model):
chemical_name = char(50)
value =char(50)
unit = choices(UNIT_CHOICES)
method = choices(METHOD_CHOICES)
CSUSNormal = char(50)
CSUSCaution = char(50)
In admin page this model consist of 180 records. Now i want to apply ordering for these records by creating field order_num. how can i set the ordering based on the order_num of each field?
I admin page i want to see all the records based on the order_num field. How can i achieve this?
Meta.ordering
class TestResult(models.Model):
chemical_name = models.CharField(max_length=50)
value =models.CharField(max_length=50)
unit = models.CharField(choices=UNIT_CHOICES)
method = models.CharField(METHOD_CHOICES)
CSUSNormal = models.CharField(max_length=50)
CSUSCaution = models.CharField(max_length=50)
order_num=models.IntegerField()
class Meta:
ordering=" order_num"
Related
My model looks like this.
class Student(models.Model):
user = models.OneToOneField(CustomUser, on_delete=models.CASCADE)
roll_no = models.CharField(max_length=32)
course = models.CharField(max_length=120)
Now I want to make a filter form using django_filters and want to use distinct values of course field as choices of select input but it requires each value to be associated with unique id and this field doesn't have any unique id.
I tried this:
class StudentFilter(django_filters.FilterSet):
course = django_filters.ModelChoiceFilter(queryset=Student.objects.values("course",flat = True).distinct(),empty_label=('Course'))
class Meta:
model = Student
fields = []
but it didn't work.
Note I do not want to make separate model for course.
The AllValuesFilter does exactly what you are asking for I believe
class StudentFilter(django_filters.FilterSet):
course = django_filters.AllValuesFilter(field_name="course")
class Meta:
model = Student
fields = []
model.py
class LabTable(models.Model):
LabNo = models.AutoField(primary_key=True)
Pid = models.IntegerField()
Weight = models.IntegerField()
DoctorId = models.ForeignKey(DoctorTable,on_delete=models.CASCADE)
Date = models.DateField()
Category = models.CharField(max_length=50)
PatientType = models.CharField(max_length=50)
Amount = models.IntegerField()
class DoctorTable(models.Model):
DoctorId = models.AtuoField(primary_key = True)
DoctorName = models.CharField(max_length = 50)
Department = models.CharField(max_length = 100)
1.Here, I have created the lab model and a foreign key with doctor table. I want to use doctor table to present data in dropdown.
You can specify how to show a DoctorTable model object by implementing the __str__ method for that model, so:
class DoctorTable(models.Model):
DoctorId = models.AutoField(primary_key=True)
DoctorName = models.CharField(max_length=50)
Department = models.CharField(max_length=100)
def __str__(self):
return self.DoctorName
Note: normally the name of the fields in a Django model are written in snake_case, not PascalCase, so it should be: doctor_name instead of DoctorName.
Note: Models normally have no Table suffix. A model is not a table, it is stored in a relational database as a table, but even then it has extra logic like validators, managers, etc.
The requirement is "I want to insert person with the person groups selection and also at the time of Creating person group I can choose persons for that particular group".
I've added two models in my models.py and manage many to many relationship between.
models.py
from django.db import models
class PersonGroup(models.Model):
id = models.AutoField(primary_key=True)
groupName = models.CharField(max_length=30)
detail = models.CharField(max_length=200)
class Person(models.Model):
id = models.AutoField(primary_key=True)
personId = models.CharField(max_length=20)
personName = models.CharField(max_length=20)
state = models.IntegerField()
personGroup = models.ManyToManyField(PersonGroup, related_name="person_list", blank=True)
serializers.py
class PersonSerializer(serializers.ModelSerializer):
personGroup = serializers.PrimaryKeyRelatedField(queryset=PersonGroup.objects.all(), many=True)
class Meta:
model = Person
fields = '__all__'
class PersonGroupSerializer(serializers.ModelSerializer):
person_list = PersonSerializer(many=True, read_only=True)
class Meta:
model = PersonGroup
fields = '__all__'
The above code help me to create person with personGroup selection
But, I also want to add persons selection at the time of create personGroup. Currently at the time of creating personGroup I'm not allowed to enter persons.
Please let me know if there any solution by which I can also select available persons at the time of person group creation.
Your person_list field in the PersonGroupSerializer is on read only, so you can't modify it using the API.
person_list = serializers.PrimaryKeyRelatedField(queryset=Person.objects.all(), many=True)
Try removing this arg.
You might also want to switch to a ForeignKey field instead of slugged.
I'm new in django. I want to create a table that includes some fields calculated from other model's field.
class Student(models.Model):
name = models.Charfield(max_length = 30)
...
class Subject(models.Model):
student = models.ForeignKey(Student, related_name = "student_subject")
point = models.IntegerField(default = 0)
...
How can I create a queryset to get average point for each student?
The relevant documentation is: https://docs.djangoproject.com/en/1.11/topics/db/aggregation/
from django.db.models import Avg
students = Student.objects.annotate(Avg('subject__point'))
I'm using django-tables-2 for a project. I have a table that uses a model for displaying data but I need to add one more column to display some informations from another table. Can I do that?
Have you tried the following?
# models.py
class Person(models.Model):
" This is your regular model "
first_name = models.CharField(max_length=200)
last_name = models.CharField(max_length=200)
user = models.ForeignKey("auth.User")
dob = models.DateField()
class PersonTable(tables.Table):
id = tables.Column(name="id") # just add a field here
class Meta:
model = Person
You map the column either by having the same name of the model's attribute, either using the accessor property.
I guess in your case it would be:
class UsersTable(tables.Table):
custom = Column(_("Custom"), accessor='id', orderable=False) # Any attr will do, dont mind it
def render_custom(self, record):
return services.get_some_info(record)
class Meta:
model = User
fields = (b'username', )