I'm trying to access some fields from another model in my serializer but I can't get it working by any means. Here is how I'm going about this.
Here are my models:
class Category(models.Model):
name = models.CharField(max_length=256)
class Meta:
ordering = ('name',)
def __str__(self):
return self.name
class Contact(models.Model):
first_name = models.CharField(max_length=256)
last_name = models.CharField(max_length=256)
address = models.CharField(max_length=1024)
city = models.CharField(max_length=256)
country = models.ForeignKey(Country, on_delete=models.CASCADE)
email = models.EmailField(max_length=256, unique=True)
phone = models.CharField(max_length=256)
category = models.ForeignKey(Category, on_delete=models.CASCADE)
user = models.ForeignKey(User, on_delete=models.CASCADE)
integration = models.ForeignKey(Integration, null=True, on_delete=models.SET_NULL)
def __str__(self):
return self.first_name
class Company(models.Model):
name = models.CharField(max_length=256)
address = models.CharField(max_length=256)
city = models.CharField(max_length=256)
country = models.ForeignKey(Country, on_delete=models.CASCADE)
user = models.ForeignKey(User, on_delete=models.CASCADE)
def __str__(self):
return self.name
class ContactCompany(models.Model):
contact = models.ForeignKey(Contact, on_delete=models.CASCADE, related_name='job')
company = models.ForeignKey(Company, on_delete=models.CASCADE, related_name='company')
job = models.TextField(blank=True, help_text='Job', max_length=5000, null=True)
started_at = models.DateTimeField(auto_now_add=True)
finished_at = models.DateTimeField(auto_now=True)
def __str__(self):
return self.job
And here is my serializer
class ContactSerializer(serializers.ModelSerializer):
country_name = serializers.CharField(source='country.name')
category_name = serializers.CharField(source='category.name')
user_name = serializers.CharField(source='user.email')
integration_name = serializers.CharField(source='integration.name')
class Meta:
model = Contact
fields = ('first_name', 'last_name', 'address', 'city', 'country_name', 'email', 'phone',
'category_name', 'user_name', 'integration_name', 'job', )
How can I access the company field from the ContactCompany model in this serializer?
Here is how the API response looks like
{
"first_name": "First name",
"last_name": "Last name",
"address": "Address",
"city": "City",
"country_name": "Gabon",
"email": "saidsadiasida#gmam.com",
"phone": "0712345678",
"category_name": "TestCategory",
"user_name": "ekartdragos#cyberaxo.com",
"integration_name": "testmonth",
"job": [
4
]
}
How can I get the job text be desplayed instead of the id?
One possible solution is to used serializerMethodField and get your desire information.
class ContactSerializer(serializers.ModelSerializer):
country_name = serializers.CharField(source='country.name')
category_name = serializers.CharField(source='category.name')
user_name = serializers.CharField(source='user.email')
integration_name = serializers.CharField(source='integration.name')
company = serializers.SerializerMethodField()
class Meta:
model = Contact
fields = ('first_name', 'last_name', 'address', 'city', 'country_name', 'email', 'phone',
'category_name', 'user_name', 'integration_name', 'job', 'company' )
def get_company(self, obj):
"""
at first we get the instance of ContactCompany from the id of object. then get the company information
"""
try:
contact_company = ContactCompany.objects.get(contact=obj.id)
expect ContactCompany.DoesNotExist:
# return or raise your desire this
return contact_company.company # here we have company id of this contact, also we can generate all company information from this.
As you did with the others one: its Foreign key on ContactCompany and related_name = 'company'
class ContactSerializer(serializers.ModelSerializer):
company_name = serializers.CharField(source='company.name')
company_ address = serializers.CharField(source='company.name')
Related
So I have UserAccount model, which has this fields:
class UserAccount(AbstractBaseUser, PermissionsMixin):
email = models.EmailField(max_length=255, unique=True)
iin = models.CharField(max_length=255, unique=True)
first_name = models.CharField(max_length=255)
last_name = models.CharField(max_length=255)
father_name = models.CharField(max_length=255)
phone_number = models.CharField(max_length=255)
is_active = models.BooleanField(default=True)
is_staff = models.BooleanField(default=False)
is_superuser = models.BooleanField(default=False)
is_doctor = models.BooleanField(default=False)
objects = UserAccountManager()
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = ['iin', 'first_name', 'last_name','father_name', 'phone_number']
def get_full_name(self):
return self.first_name + ' ' + self.last_name + ' ' + self.father_name
def __str__(self):
return self.email
And also this class that has user as a foreign key:
class Patient(models.Model):
user = models.ForeignKey(UserAccount, on_delete=models.CASCADE)
district = models.CharField(max_length=255)
def __str__(self):
return self.user.email
So my question is how to create a serializer for the registration? How am I supposed to create a UserAccount and Patient which has UserAccount as a foreign key together???
PLS HELP This is my diploma work
THIS DOES NOT WORK
class RegistrationSerializer(serializers.ModelSerializer):
class PatientCreateSerializer(serializers.ModelSerializer):
class Meta:
model = Patient
exclude = ['user']
patient = PatientCreateSerializer()
class Meta:
model = UserAccount
fields = '__all__'
def create(self, validated_data):
patient_data = validated_data.pop('patient')
user_instance = UserAccount.objects.create(**validated_data)
Patient.objects.create(user=user_instance,**patient_data)
return user_instance
Also, how am I supposed to send data in POSTMAN?
LIKE THIS?
{
"patient": {
"district": "2323232",
"user": [{
"email": "someemail#gmail.com",
"iin": "02020202002",
"first_name": "Some",
"last_name": "Somee",
"father_name": "Someuly",
"phone_number": "+72783928932",
"password": "password",
"re_password": "password"
}]
}
}
All OK, but PatientCreateSerializer shouldn't place inside
For example
class PatientCreateSerializer(serializers.ModelSerializer):
class Meta:
model = Patient
exclude = ['user']
class RegistrationSerializer(serializers.ModelSerializer):
patient = PatientCreateSerializer()
class Meta:
model = UserAccount
fields = '__all__'
def create(self, validated_data):
patient_data = validated_data.pop('patient')
user_instance = UserAccount.objects.create(**validated_data)
Patient.objects.create(user=user_instance,**patient_data)
return user_instance
And add in patient.user foreign key params related_name=patiend.
If will be errors like "patient in not fields" - description fields by hand
In my django app, i have four model tables linked using foreign keys, the problem is, when i make a query for any model table, the fields that are linked through foreign keys are returned as id's instead of the name.
My way does not work.
My customers models.py file
1st model
class Customer(models.Model):
name = models.CharField(max_length=50)
phone = models.CharField(max_length=20, unique=True)
email = models.EmailField(max_length=255, unique=True, blank=True)
image = models.ImageField(default='default.png', upload_to='customer_photos/%Y/%m/%d/')
data_added = models.DateField(default=datetime.now, blank=True)
def __str__(self):
return self.name
2nd model
class ShippingAddress(models.Model):
customer = models.ForeignKey(Customer, on_delete=models.CASCADE, related_name="customer_ship_address")
description = models.TextField(blank=True)
frequent_customer = models.BooleanField(default=False)
address = models.CharField(max_length=50, blank=True)
zip_code = models.CharField(max_length=12, blank=True)
location = models.CharField(max_length=255)
def __str__(self):
return self.customer.name
3rd model - PaymentInvoice
class paymentInvoice(models.Model):
shipping_address_owner = models.ForeignKey(
ShippingAddress, on_delete=models.CASCADE, related_name="customer_invoice")
product = models.ManyToManyField(
Product, related_name='product_invoice')
mode = models.CharField(max_length=20, choices=paymentMode.choices, default=paymentMode.MPESA)
date = models.DateField(default=datetime.now)
invoice_id = models.CharField(
max_length=50, unique=True, default=increment_invoice_number)
quantity = models.PositiveSmallIntegerField()
status = models.CharField(
max_length=20, choices=paymentStatus.choices, default=paymentStatus.PENDING)
payment_made = models.DecimalField(max_digits=20, decimal_places=2)
def __str__(self):
return self.shipping_address_owner.customer.name
My Products models.py file
class Product(models.Model):
slug = models.CharField(max_length=200, unique=True)
name = models.CharField(max_length=200)
available = models.BooleanField(default=True)
description = models.TextField(blank=True)
image = models.ImageField(default='default_product.jpg', upload_to='product_photos')
category = models.CharField(max_length=200)
qty_amount = models.CharField(
max_length=20, choices=Qty_Choices, default='250ml')
price = models.DecimalField(max_digits=10, decimal_places=2)
def __str__(self):
return self.name
My PaymentInvoice views.py file
class paymentInvoiceListCreateView(ListCreateAPIView):
"""
ListCreateAPIView executes both 'GET' and 'POST' requests. i.e listing a queryset or creating a model instance.
"""
serializer_class = paymentInvoiceSerializer
queryset = paymentInvoice.objects.all().order_by(
'-date').values(shipping_address_owner__customer)
When i make the above query the api returns the following, where the product field and shipping_address_owner field are just id's. I need there respective names instead.
{
"count": 6,
"next": null,
"previous": null,
"results": [
{
"id": 9,
"mode": "Mpesa",
"date": "2020-07-27",
"invoice_id": "INV-0006",
"quantity": 1,
"status": "Pending",
"payment_made": "500.00",
"shipping_address_owner": 9,
"product": [
1
]
},
EDIT: paymentInvoiceSerializer
class paymentInvoiceSerializer(serializers.ModelSerializer):
class Meta:
model = paymentInvoice
fields = '__all__'
You need to make some changes to your serializer.
If you just need the name and nothing more:
class paymentInvoiceSerializer(serializers.ModelSerializer):
product = serializers.SerializerMethodField()
shipping_address_owner = serializers.SerializerMethodField()
class Meta:
model = paymentInvoice
fields = '__all__'
def get_product(self, instance):
names = []
for product in instance.product.all():
names.append(product.name)
return names
def get_shipping_address_owner(self, instance):
return instance.shipping_address_owner.customer.name
You can also create different serializer for each model and pass them to their fields to get the full serialized data for them. It would be something like this:
ProductSerializer(serializers.ModelSerializer):
class Meta:
model = Product
fields = '__all__'
And then in your payment serializer:
class paymentInvoiceSerializer(serializers.ModelSerializer):
product = ProductSerializer()
class Meta:
model = paymentInvoice
fields = '__all__'
You can do the same for shipping_address_owner.
You can try like this in your serializer.
class paymentInvoiceSerializer(serializers.ModelSerializer):
shipping_address_owner = serializers.CharField(source='shipping_address_owner.customer')
class Meta:
model = paymentInvoice
fields = ['shipping_address_owner',...]
I have four models where I need to serialize three into one for my API. I can get the individual serializers to work as expected, but when combining them into one, I do not get the result I was expecting to see.
models.py
class President(models.Model):
name = models.CharField('Name', max_length=50,)
staff = models.ManyToManyField('Member',)
def __str__(self):
return self.name
"""
user creates a new member in this model below
if a new member is an employee, the object is copied into the Employee model
and the user chooses the manager of the employee in the manager field
if a new member is a manager, the object is copied into the Manager model
"""
class Member(models.Model):
president = models.ForeignKey(
President, on_delete=models.CASCADE, related_name='members',
)
manager = models.ForeignKey(
'Manager', on_delete=models.CASCADE, related_name='manager',
)
name = models.CharField('Name', max_length=50,)
email = models.EmailField('Email Address',)
title = models.CharField('Title', max_length=50,)
staff_type = (
('Manager', 'Manager'),
('Employee', 'Employee'),
)
def __str__(self):
return self.name
class Employee(models.Model):
president = models.ForeignKey(
President, on_delete=models.CASCADE, related_name='employeePresident'
)
manager = models.ForeignKey(
'Manager', on_delete=models.CASCADE, related_name='employeeManager'
)
name = models.CharField('Name', max_length=50,)
email = models.EmailField('Email Address',)
title = models.CharField('Title', max_length=50,)
def __str__(self):
return self.name
class Manager(models.Model):
president = models.ForeignKey(
President, on_delete=models.CASCADE, related_name='managerPresident'
)
name = models.CharField('Name', max_length=50,)
department = models.CharField('Department', max_length=50,)
def __str__(self):
return self.name
serializers.py
class PresidentSerializer(serializers.ModelSerializer):
class Meta:
model = President
fields = '__all__'
class EmployeeSerializer(serializers.ModelSerializer):
class Meta:
model = Employee
fields = '__all__'
class ManagerSerializer(serializers.ModelSerializer):
members = EmployeeSerializer(many=True,)
class Meta:
model = Manager
fields = ('president', 'name', 'department', 'members')
Up to this point all of these serializers work as expected. The ManagerSerializer exposes the manager's name and the employees underneath them in a tree view like I want.
But, an employee does not necessarily have to be under a manager, for instance they could report directly to the President object, like a Presidents assistant.
How would I combine these three serializers into one where my API would be as follows:
{
"name": "Presidents Name",
"staff": [
{
"name": "Employee No Manager",
"title": "Presidents Asst"
},
{
"name": "John Doe",
"title": "Finance Manager",
"employees": [
{
"name": "Mary Simpson",
"title": "Finance Asst"
}
]
}
}
Not sure if something needs to change in my model design where the President model can take in both Employee and Manager models. Any help would be greatly appreciated.
I would simplify your db structure to just 1 class for every employee (and 2 extra classes for db normalization):
class JobTitle(models.Model):
title = models.CharField('Title', max_length=50)
def __str__(self):
return self.title
class Department(models.Model):
name = models.CharField('Department', max_length=50)
def __str__(self):
return self.name
class Employee(models.Model):
name = models.CharField('Name', max_length=50)
boss = models.ForeignKey("Employee", on_delete=models.CASCADE, related_name="staff", blank=True, null=True)
department = models.ForeignKey("Department", on_delete=models.CASCADE)
email = models.EmailField('Email Address')
title = models.ForeignKey("JobTitle", on_delete=models.CASCADE)
#property
def is_president(self):
return self.boss is None
#property
def is_manager(self):
return len(self.staff) > 0
def __str__(self):
return self.name
Then you can use recursive serializer from this answer:
class RecursiveField(serializers.Serializer):
def to_representation(self, value):
serializer = self.parent.parent.__class__(value, context=self.context)
return serializer.data
class EmployeeSerializer(serializers.ModelSerializer):
staff = RecursiveField(many=True)
class Meta:
model = Employee
fields = '__all__'
I am creating an api where the list of groups are shown along with the devices id that falls under that groups. For example if there is a device named Speedometer, Humidifier and they fall under Home group then my api should include
{
"id": 1,
"name": "Home"
"device_list": [
{
"id": "b45c56ioxa1"
},
{
"id": "h4oc2d5ofa9"
}
]
},
but my code does not produce device_list in the api. It only shows name and id
device_list is the list of all the devices id that are in a certain group.
Here is my code
class DeviceIdSerializer(serializers.ModelSerializer):
id = serializers.UUIDField(source='token', format='hex', read_only=True)
class Meta:
model = Device
fields = ('id')
class DeviceGroupSerializer(serializers.ModelSerializer):
name = serializers.StringRelatedField()
device_list = DeviceIdSerializer(read_only=False, many=True, required=False, source="groups")
class Meta:
model = DeviceGroup
fields = ('id', 'name', 'device_list')
class DevicesGroupsAPIView(APIView):
permission_classes = (permissions.IsAuthenticated,)
def get(self, request, format=None):
"""
Returns a list of groups
"""
reply = {}
try:
groups = DeviceGroup.objects.all()
print ('reply', groups)
reply['data'] = DeviceGroupSerializer(groups, many=True).data
except:
reply['data'] = []
return Response(reply, status.HTTP_200_OK)
class BaseDevice(PolymorphicModel):
# User's own identifier of the product
name = models.CharField(max_length=250, blank=False, null=False)
# Any device should have a owner, right from the creation
owner = models.ForeignKey(User, blank=False, null=False)
token = models.UUIDField(default=uuid.uuid4, unique=True, editable=False)
group = models.ForeignKey('DeviceGroup', related_name="groups", null=True, blank=True)
class Device(BaseDevice):
description = models.TextField(blank=True, null=True)
class DeviceGroup(models.Model):
name = models.CharField(max_length=250, blank=False, null=False)
I tried out the very same code you have except I used models.Model as the base model.
The first time I got an error
The fields option must be a list or tuple or "all". Got str.
which clearly states where your problem is.
So I changed class the fields option in DeviceIdSerializer
DeviceIdSerializer(serializers.ModelSerializer):
id = serializers.UUIDField(source='token', format='hex', read_only=True)
class Meta:
model = Device
fields = ('id',)
Note that I've added a comma (",") which makes fields a tuple instead of a string as it was before.
Now the data I get is
{
"id":1,
"name":"test",
"device_list":[
{
"id":"38ec7e152f9d49a38008c859a1022525"
},
{
"id":"b0d799509260474cb092899ef84ce49c"
},
{
"id":"e5c7cf8f9f5043c68c34c7b962569b08"
}
]
}
which is the same as what you are looking for...
I think your serializers need to look like this:
class DeviceIdSerializer(serializers.ModelSerializer):
id = serializers.UUIDField(source='token', format='hex', read_only=True)
class Meta:
model = Device
fields = ('id')
class DeviceGroupSerializer(serializers.ModelSerializer):
name = serializers.StringRelatedField()
groups = DeviceIdSerializer(read_only=False, many=True, required=False)
class Meta:
model = DeviceGroup
fields = ('id', 'name', 'groups')
Or change this:
class BaseDevice(PolymorphicModel):
# User's own identifier of the product
name = models.CharField(max_length=250, blank=False, null=False)
# Any device should have a owner, right from the creation
owner = models.ForeignKey(User, blank=False, null=False)
token = models.UUIDField(default=uuid.uuid4, unique=True, editable=False)
group = models.ForeignKey('DeviceGroup', related_name="device_list", null=True, blank=True)
I am using Django 1.10.5 and djangorestframework 3.5.3.
I have 2 models that are related with one to many relation:
class Minisymposium(models.Model):
STATUS_CHOICES = (
('pending', 'Pending'),
('approved', 'Approved'),
('denied', 'Denied'))
id = models.AutoField(primary_key=True)
number = models.IntegerField(default=0, null=False)
title = models.CharField(max_length=100, null=False)
description = models.TextField(max_length=9000, null=False)
status = models.CharField(max_length=100, choices=STATUS_CHOICES, null=False, default='pending')
user = models.ForeignKey(User, null=False, related_name='user')
corresponding_organizer = models.ForeignKey(User, null=False, related_name='corresponding_organizer')
anticipated_abstracts = models.IntegerField(null=False)
anticipated_attendees = models.IntegerField(null=False)
date = models.DateTimeField(auto_now_add=True)
def __str__(self):
return '{0}-{1}'.format(self.number, self.title)
class UnregisteredOrganizer(models.Model):
id = models.AutoField(primary_key=True)
first_name = models.CharField(max_length=1000, blank=False)
last_name = models.CharField(max_length=1000, blank=False)
email = models.EmailField(max_length=254, blank=True)
affiliation = models.CharField(max_length=254, help_text='(institution only)', blank=True)
phone_number = PhoneNumberField(blank=True)
minisymposium = models.ForeignKey(Minisymposium, related_name='other_organizers')
Each model have a serializer. But the problem is with Minisymposium`s serializer. Because I want to send an UnregisteredOrganizer`s ID on creating one, and get the whole object as serialized on getting a Minisymposium.
And as I see in ModelSerializer it is not possible:
class MinisymposiumSerializer(serializers.ModelSerializer):
other_organizers = UnregisteredOrganizerSerializer(many=True)
class Meta:
model = Minisymposium
fields = ('url', 'id', 'number', 'title', 'description', 'status', 'user', 'corresponding_organizer',
'anticipated_abstracts', 'anticipated_attendees', 'other_organizers', 'date')
def create(self, validated_data):
other_organizers = []
if 'other_organizers' in validated_data:
other_organizers = validated_data.pop('other_organizers')
minisymposium = Minisymposium.objects.create(**validated_data)
minisymposium.save()
for organizer in other_organizers:
UnregisteredOrganizer.objects.create(minisymposium=minisymposium, **organizer).save()
return minisymposium
How can I do that?
Thank you !
Because I want to send an UnregisteredOrganizer`s ID on creating one, and get the whole object as serialized on getting a Minisymposium.
Why have such an inconsistent API ?
The recommended option here is to set the fields on Minisymposium as read_only except for the id which should be read_only=False.
Therefore, you can get the full object when getting the data and just expect the id when post/put/patching the data. Posted JSON would look like:
{
...
"url": "whatever",
"title": "Some nice title",
"other_organizers": [{"id": 5}, {"id": 5123}]
}
Creation code would be like:
def create(self, validated_data):
other_organizers = validated_data.pop('other_organizers', [])
minisymposium = Minisymposium.objects.create(**validated_data)
minisymposium.save()
organizers = []
for organizer_id in other_organizers:
organizers .append(UnregisteredOrganizer.objects.get(id=organizer_id)
minisymposium. other_organizers = organizers
return minisymposium