I have this situation:
Model Handling
class Handling(models.Model):
STATUS = (
('Active', 'Active'),
('Archived', 'Archived'),
)
entdate = models.DateTimeField(auto_now_add=True, null=True)
extdate = models.DateTimeField(auto_now_add=True, null=True)
kpallet = models.ForeignKey(Pallet, related_name='kpallet', null=True, on_delete= models.PROTECT)
kitem = models.ForeignKey(Item,related_name='kitems', null=True, on_delete= models.PROTECT, limit_choices_to={'kstatus': 'Active'})
quantity = models.SmallIntegerField(null=True)
kstatus = models.CharField(max_length=20, null=True, choices=STATUS)
def __str__(self):
return str(self.kpallet)
Model Item:
class Item(models.Model):
STATUS = (
('Active', 'Active'),
('Disabled', 'Disabled'),
('Archived', 'Archived'),
)
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
name = models.CharField(max_length=50, null=True)
description = models.CharField(max_length=200, null=True)
kdimension = models.ForeignKey(Dimension, null=True, on_delete= models.PROTECT)
kclient = models.ForeignKey(Client, null=True, on_delete= models.PROTECT)
kstatus = models.CharField(max_length=20, null=True, choices=STATUS)
def __str__(self):
return self.name
Serializer:
class HandlingSerializer(serializers.ModelSerializer):
class Meta:
model = Handling
fields = '__all__'
Api:
#api_view(['POST'])
#permission_classes((permissions.AllowAny,))
def handlingCreate(request):
serializer = HandlingSerializer(data=request.data)
if serializer.is_valid():
serializer.save()
else:
print(serializer.errors);
return Response("Error Handling not created")
return Response("Handling Created")
I get this error and i don't understand how to move on:
{'kitem': [ErrorDetail(string='Invalid pk "958c2fd2-bbb6-42d6-8bfe-fbe035e9ceb5" - object does not exist.', code='does_not_exist')]}
I've checked the pk and the object exists so I don't understand where the issue could be.
Thanks for your help in advance.
Fixed thanks to Blackdoor for the input.
This is the correct serializer:
class HandlingSerializer(serializers.ModelSerializer):
kitem = serializers.PrimaryKeyRelatedField(queryset=Item.objects.all(), pk_field=serializers.UUIDField(format='hex_verbose'))
class Meta:
model = Handling
fields = '__all__'
use pk_field=UUIDField for PrimaryKeyRelatedField
class HandlingSerializer(serializers.ModelSerializer):
kitem = serializers.PrimaryKeyRelatedField(queryset=Item.objects.all(), pk_field=UUIDField(format='hex'))
class Meta:
model = Handling
fields = '__all__'
Related
While creating new products I'm getting such kind of error. Can someone help me?
class Product(models.Model):
user = models.ForeignKey(User, on_delete=models.SET_NULL, null=True)
name_geo = models.CharField(max_length=200, null=True, blank=True)
image = models.ImageField(null=True, blank=True, default='/placeholder.png')
brand = models.CharField(max_length=200, null=True, blank=True)
category = models.ForeignKey(Category, null=False, default=0, on_delete=models.CASCADE)
price = models.DecimalField(max_digits=7, decimal_places=2, null=True, blank=True)
countInStock = models.IntegerField(null=True, blank=True, default=0)
createdAt = models.DateTimeField(auto_now_add=True)
_id = models.AutoField(primary_key=True, editable=False)
def __str__(self):
return self.name_geo
class Category(models.Model):
_id = models.AutoField(primary_key=True, editable=False)
name = models.CharField(max_length=200, null=True, blank=True)
createdAt = models.DateTimeField(auto_now_add=True)
def __str__(self):
return self.name
#api_view(['POST'])
def createProduct(request):
user = request.user
product = Product.objects.create(
user=user,
name_geo="Sample Name",
category="Sample Category",
price=0,
brand='Sample Brand',
countInStock=0,
)
serializer = ProductSerializer(product, many=False)
return Response(serializer.data)
Without separating category class in models.py everything works fine. I mean If i didn't use ForeignKey in Products class for category
It just has to be a Category Instance/Object
product = Product.objects.create(
user=user,
name_geo="Sample Name",
category=Category.objects.get_or_create(name="Sample Category"),
price=0,
brand='Sample Brand',
countInStock=0,
)
Notes:
You could just do a .get() or a .filter().first() if you don't want to create
If you use a form, you can get away with just the Category's PK/_id in the POST
this type of thing: f = form(request.POST) f.is_valid() f.save()
At the end that field will hold the PK/_id/Row# of the Category Obj
How do I handle this serializer method field? I feel that using initial_data is wrong, do i need to validate data when extracting it from the database? Do I need to use a serializer when extracting data from database?
class ListingSerializer(serializers.ModelSerializer):
rooms = serializers.SerializerMethodField()
# This one works as expected
def get_rooms(self, obj):
rooms = list(Room.objects.filter(listing__id=obj.id).values())
serializer = RoomSerializer(data=rooms, many=True)
return serializer.initial_data
# This one gives serializer errors
def get_rooms(self, obj):
rooms = list(Room.objects.filter(listing__id=obj.id).values())
serializer = RoomSerializer(data=rooms, many=True)
if serializer.is_valid():
return serializer.data
return serializer.errors
class Meta:
model = Listing
fields = "__all__"
class Listing(models.Model):
id = models.CharField(
primary_key=True, default=generate_uuid, editable=False, max_length=36
)
agent = models.ForeignKey(
"users.User", on_delete=models.CASCADE, blank=True, null=True
)
# property data
title = models.CharField(max_length=100, blank=False, null=False)
description = models.CharField(max_length=1000, blank=False, null=False)
floor = models.IntegerField(blank=False, null=False)
floor_count = models.IntegerField(blank=False, null=False)
price = models.DecimalField(max_digits=10, decimal_places=2, default=Decimal(0.00))
# address
street = models.CharField(max_length=60, blank=False, null=True)
house_no = models.CharField(max_length=10, blank=False, null=True)
door_no = models.CharField(max_length=10, blank=False, null=True)
city = models.CharField(max_length=20, blank=False, null=True)
country = models.CharField(max_length=20, blank=False, null=True)
postal_code = models.IntegerField(blank=False, null=True)
# property reports
tilstand_report = models.FileField(upload_to="reports", blank=False, null=True)
water_consumption_report = models.FileField(
upload_to="reports", blank=False, null=True
)
energy_level_report = models.FileField(upload_to="reports", blank=False, null=True)
property_tax_report = models.FileField(upload_to="reports", blank=False, null=True)
# metadata
is_active = models.BooleanField(default=True)
create_time = models.BigIntegerField(blank=True, null=True)
def save(self, *args, **kwargs):
self.create_time = int(datetime.now().timestamp() * 1000)
super().save(*args, **kwargs)
class Meta:
ordering = ["create_time"]
You can use the RoomSerializer as a sub serializer:
class ListingSerializer(serializers.ModelSerializer):
rooms = RoomSerializer(source='room_set', many=True)
class Meta:
model = Listing
fields = '__all__'
The source=… should specify the related_name=… of the ForeignKey in the Room model. If you did not specify a related_name=…, the default is modelname_set, so here room_set.
I have a cartmodel, cartitem and offlinecheckout model. I want to display cartitem instead of cartmodel id, I want to display all the cartitem data which have cart_id = offlinecheckout cart_id. But I got this response.
I tried a lot but didn't get. Anybody will please help me.
views.py
class GetAPI(APIView):
def get(self, request, *args, **kwargs):
serializer = OfflineSerializer()
return Response(serializer.data)
models.py
class OfflineCheckOut(models.Model):
billing_name = models.CharField(max_length=254)
billing_phone_no = models.CharField(max_length=15)
user = models.ForeignKey('accounts.User', on_delete=models.CASCADE)
cart = models.ForeignKey('cart.CartModel', on_delete=models.CASCADE)
cartitem = models.ManyToManyField(CartItem, blank=True)
# time_slot = models.ForeignKey('category.TimeSlot', on_delete=models.CASCADE)
address = models.ForeignKey('cart.CustomerAddress', on_delete=models.CASCADE)
status_choice = [
('0', 'Offline'),
('1', 'Online')
]
status = models.CharField(max_length=3, choices=status_choice, default=0)
# date = models.DateField()
date = models.DateField()
time_slot = models.ForeignKey('category.TimeSlot', on_delete=models.SET_NULL, null=True, blank=True)
order_id = models.CharField(max_length=254, blank=True)
# date = models.DateField()
razorpay_payment_id =models.CharField(max_length=254, blank=True)
razorpay_signature = models.CharField(max_length=254, blank=True)
paid = models.BooleanField(default=False)
service = models.ForeignKey('service.ServiceProvider', on_delete=models.SET_NULL, null=True, blank=True)
class CartModel(models.Model):
user = models.ForeignKey('accounts.User', on_delete=models.CASCADE)
status_choice = [
('1', 'open'),
('2', 'closed')
]
status = models.CharField(max_length=2, choices=status_choice, default=1)
validated = models.BooleanField(default=False)
def __str__(self):
return self.user.username
#property
def total_price(self):
return self.cartitem_set.aggregate(
total_price=Sum(F('quantity') * F('price'))
)['total_price'] or Decimal('0')
class CartItem(models.Model):
cart = models.ForeignKey('CartModel', on_delete=models.CASCADE)
user = models.ForeignKey('accounts.User', on_delete=models.CASCADE)
service = models.ForeignKey('accounts.SubCategory', on_delete=models.CASCADE)
defects = models.ForeignKey('category.Defects', on_delete=models.CASCADE)
quantity = models.IntegerField(default=1)
price = models.IntegerField()
created_on = models.DateTimeField(auto_now_add=True)
updated_on = models.DateTimeField(auto_now_add=True)
order_placed = models.BooleanField(default=False)
serializers.py
class OfflineSerializer(serializers.ModelSerializer):
def total(self, obj):
return obj.total
total = serializers.IntegerField(read_only=True)
cartitems = CartItemSerializer( read_only=True, many=True)
class Meta:
model = OfflineCheckOut
fields = ['user', 'billing_name','billing_phone_no','cartitem', 'cartitems','cart', 'date', 'time_slot', 'address', 'total']
extra_fields = ['total', 'cartitems']
You have to put them in a serializer for CartModel. Then OfflineSerializer will use that for cart field.
class CartModelSerializer(serializers.ModelSerializer):
cartitem_set = CartItemSerializer(read_only=True, many=True)
class Meta:
model = CartModel
fields = [
"cartitems_set",
# other fields here
]
class OfflineSerializer(serializers.ModelSerializer):
def total(self, obj):
return obj.total
total = serializers.IntegerField(read_only=True)
cart = CartModelSerializer(read_only=True)
class Meta:
model = OfflineCheckOut
fields = ['user', 'billing_name','billing_phone_no','cartitem', 'cartitems','cart', 'date', 'time_slot', 'address', 'total']
extra_fields = ['total', 'cartitems']
EDIT: Changed the related name serializer from cartlineitems_set to cartlineitem_set
I have a Project and Memberships model where each project has a list of members. I need to find out if the current logged in user is listed as a member on the project from the project serializer, but I can't seem to get the request correct in my get_i_am_member method. I need to pass the project id from the Project model to the Membership model to filter the data, then check if the user in the filtered memberships model matches the user making the request. Can someone please assist? Here is my code:
######################################################
# Serializer
######################################################
class ProjectSerializer(LightWeightSerializer):
id = Field()
name = Field()
slug = Field()
description = Field()
created_date = Field()
modified_date = Field()
owner = MethodField()
members = MethodField()
is_private = Field()
anon_permissions = Field()
public_permissions = Field()
is_looking_for_people = Field()
looking_for_people_note = Field()
i_am_member = MethodField()
i_am_admin = MethodField()
my_permissions = MethodField()
def get_members(self, project):
members = Membership.objects.filter(project_id=project.id).select_related()
return MembershipSerializer(members, many=True, context=self.context).data
def get_i_am_member(self, request):
members_list = Membership.objects.filter(project_id=request.project.id).select_related('user')
for member in members_list:
if member.user == request.username:
print(member.user)
print("True")
return True
else:
print(member.user)
print("False")
return False
######################################################
# Models
######################################################
class Project(models.Model):
name = models.CharField(max_length=250, null=False, blank=False,
verbose_name=_("name"))
slug = models.SlugField(max_length=250, unique=True, null=False, blank=True,
verbose_name=_("slug"))
description = models.TextField(null=False, blank=False,
verbose_name=_("description"))
logo = models.FileField(upload_to=get_project_logo_file_path,
max_length=500, null=True, blank=True,
verbose_name=_("logo"))
created_date = models.DateTimeField(null=False, blank=False,
verbose_name=_("created date"),
default=timezone.now)
modified_date = models.DateTimeField(null=False, blank=False,
verbose_name=_("modified date"))
owner = models.ForeignKey(settings.AUTH_USER_MODEL, null=True, blank=True,
related_name="owned_projects", verbose_name=_("owner"), on_delete=models.CASCADE)
members = models.ManyToManyField(settings.AUTH_USER_MODEL, related_name="projects",
through="Membership", verbose_name=_("members"),
through_fields=("project", "user"))
is_private = models.BooleanField(default=True, null=False, blank=True,
verbose_name=_("is private"))
anon_permissions = ChoiceArrayField(
models.TextField(null=False, blank=False, choices=ANON_PERMISSIONS),
null=True,
blank=True,
default=list,
verbose_name=_("anonymous permissions")
)
public_permissions = ChoiceArrayField(models.TextField(null=False, blank=False, choices=MEMBERS_PERMISSIONS),
null=True, blank=True, default=list, verbose_name=_("user permissions"))
is_featured = models.BooleanField(default=False, null=False, blank=True,
verbose_name=_("is featured"))
class Meta:
db_table = "projects"
verbose_name = "project"
verbose_name_plural = "projects"
ordering = ["name", "id"]
index_together = [
["name", "id"],
]
class Membership(models.Model):
# This model stores all project memberships. Also
# stores invitations to memberships that do not have
# assigned user.
user = models.ForeignKey(settings.AUTH_USER_MODEL, null=True, blank=True, default=None,
related_name="memberships", on_delete=models.CASCADE)
project = models.ForeignKey(Project, null=False, blank=False,
related_name="memberships", on_delete=models.CASCADE)
role = models.ForeignKey('core.Role', null=False, blank=False,
related_name="memberships", on_delete=models.CASCADE)
is_admin = models.BooleanField(default=False, null=False, blank=False)
user_order = models.BigIntegerField(default=timestamp_ms, null=False, blank=False,
verbose_name=_("user order"))
class Meta:
db_table = "memberships"
verbose_name = "membership"
verbose_name_plural = "memberships"
unique_together = ("user", "project",)
ordering = ["project", "user__full_name", "user__username", "user__email"]
def get_related_people(self):
related_people = get_user_model().objects.filter(id=self.user.id)
return related_people
def clean(self):
# TODO: Review and do it more robust
memberships = Membership.objects.filter(user=self.user, project=self.project)
if self.user and memberships.count() > 0 and memberships[0].id != self.id:
raise ValidationError(_('The user is already member of the project'))
By default drf passes request object using context, so modification should be:
def get_i_am_member(self, project):
members_list = Membership.objects.filter(project_id=project.id).select_related('user')
for member in members_list:
if member.user == self.context['request'].user:
print(member.user)
print("True")
return True
else:
print(member.user)
print("False")
return False
But in case you manually call serializer, you need to pass it:
ProjectSerializer(instance=project, context={'request': request})
I have this Model:
class Complaint(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, default=1)
date_created = models.DateTimeField(auto_now_add=True, null=True, blank=True)
name = models.CharField(max_length=255, unique=True)
definition = models.TextField(blank=False, default="")
is_violent = models.BooleanField(default=False)
is_active = models.BooleanField(default=True)
def __str__(self):
return self.name
class Meta:
ordering = ['name']
def get_absolute_url(self):
return reverse('complaint-details', kwargs={'pk': self.pk})
class Service(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, default=1)
date_created = models.DateTimeField(auto_now_add=True, null=True, blank=True)
name = models.CharField(max_length=255, unique=True)
definition = models.TextField(blank=True, default="")
is_active = models.BooleanField(default=True)
def __str__(self):
return self.name
def get_absolute_url(self):
return reverse('service-details', kwargs={'pk': self.pk})
class Location(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, default=1)
date_created = models.DateTimeField(auto_now_add=True, null=True, blank=True)
location_name = models.CharField(max_length=255, unique=True)
loc_lat = models.DecimalField(max_digits=9, decimal_places=6)
loc_long = models.DecimalField(max_digits=9, decimal_places=6)
pop = models.PositiveIntegerField(default=500)
is_AOR = models.BooleanField(default=False)
is_active = models.BooleanField(default=True)
def __str__(self):
return self.location_name
class Blotter(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, default=1)
date_created = models.DateTimeField(auto_now_add=True, null=True, blank=True)#default=timezone.now().date()
date = models.DateField(blank=True)
time = models.TimeField(blank=True)
entry_number = models.CharField(max_length=255, unique=True,validators=[RegexValidator(r'^\d{1,255}$')])
complaints = models.ForeignKey(Complaint, on_delete=models.CASCADE, null=True, blank=True)
service = models.ForeignKey(Service, on_delete=models.CASCADE, null=True, blank=True)
information = models.TextField(blank=False, default="")
location = models.ForeignKey(Location, on_delete=models.CASCADE, null=True, blank=True)
is_active = models.BooleanField(default=True)
class Meta:
ordering = ("date_created",)
def __str__(self):
return (self.entry_number)
def get_absolute_url(self):
return reverse('details-blotter', kwargs={'pk': self.pk})
And I have this serializer:
class APILocationListSerializer(serializers.Serializer):
address = serializers.CharField()
latitude = serializers.DecimalField(max_digits=9, decimal_places=5)
longitude = serializers.DecimalField(max_digits=9, decimal_places=5)
population= serializers.IntegerField()
crime_count=serializers.IntegerField()
crime_rate=serializers.DecimalField(max_digits=4, decimal_places=3)
is_aor = serializers.BooleanField()
class Meta:
model = Blotter
fields= [
'address',
'latitude',
'longitude',
'population',
'crime_count',
'crime_rate'
'is_aor',
]
def to_representation(self, value):
context = {
value['address']:
{
'coordinates':[value['latitude'],value['longitude']],
'Population': value['population'],
'Crime-Count': value['crime_count'],
'Crime-Rate': value['crime_rate'],
'Area-Of-Responsibility': value['is_aor'],
}
}
return context
And ListApiView:
class APILocationList(generics.ListAPIView):
serializer_class = APILocationListSerializer
def get_queryset(self):
q=Blotter.objects.values('location__location_name').annotate(
address=F('location__location_name'),
latitude=F('location__loc_lat'),
longitude=F('location__loc_long'),
population=F('location__pop'),
crime_count=Count('complaints', filter=Q(complaints__is_active=True) and Q(complaints__isnull=False)),
crime_rate=(Cast(F('crime_count'), FloatField())/Cast(F('population'), FloatField()))*100000,
is_aor=F('location__is_AOR')
)
q1 = q.filter(location__is_AOR=True).order_by('address')
query_search = self.request.GET.get("q")
if query_search:
q1 = q.filter(Q(location__is_AOR=True) and Q(location__location_name__icontains=query_search)).order_by('address')
return q1
I'm new to django and DRF. I want to achieve a result like this in my API Not Achieved
but this is the result that i've achieved so far Achieved
As you can see in the picture, I want to count the crime trend (highest count of a crime and the crime itself) in every location.
My questions are:
Is this even achievable/possible to get these results using just one query?
If yes, how?
If no, is there any other way to achieve these kind of results?
Thank you in advance!