How to display the user id in django - django

I have a function in which I have to, among other things, pass to the template the ids of the users who conducted the correspondence, but I get an error in the line:
pk_list = messages.values('user_from__pk').distinct()
views.py:
def send_chat(request):
resp = {}
User = get_user_model()
if request.method == 'POST':
post =request.POST
u_from = UserModel.objects.get(id=post['user_from'])
u_to = UserModel.objects.get(id=post['user_to'])
messages = request.user.received.all()
pk_list = messages.values('user_from__pk').distinct()
correspondents = get_user_model().objects.filter(pk__in=list(pk_list))
insert = chatMessages(user_from=u_from,user_to=u_to,message=post['message'],correspondents=correspondents)
try:
insert.save()
resp['status'] = 'success'
except Exception as ex:
resp['status'] = 'failed'
resp['mesg'] = ex
else:
resp['status'] = 'failed'
return HttpResponse(json.dumps(resp), content_type="application/json")
models.py:
class chatMessages(models.Model):
user_from = models.ForeignKey(User,
on_delete=models.CASCADE,related_name="sent")
user_to = models.ForeignKey(User,
on_delete=models.CASCADE,related_name="received")
message = models.TextField()
date_created = models.DateTimeField(default=timezone.now)
correspondents = models.ForeignKey(User,
on_delete=models.CASCADE,related_name="correspondents", null=True)
def __str__(self):
return self.message
Error:
TypeError: Field 'id' expected a number but got {'user_from__pk': 1}.
how can I fix this error?

problem is here:
pk_list = messages.values('user_from__pk').distinct()
queryset.values give you a list of dictionaries. in your case [{'user_from__pk': pk1}, {'user_from__pk': pk2},... e.t.c.]
more here: https://docs.djangoproject.com/en/4.1/ref/models/querysets/#values
And you need values_list.
pk_list = messages.values_list('user_from__pk', flat=True).distinct()
To avoid joins - you can do:
pk_list = messages.values_list('user_from_pk', flat=True).distinct()
more here:
https://docs.djangoproject.com/en/4.1/ref/models/querysets/#values-list

Related

201 Created - but no new object in SqLite database

Edit 25th Jan:
here's my model!
class rat(models.Model):
name = models.CharField(max_length=132, null =True)
body_colour = models.ForeignKey(BodyColour, on_delete=models.CASCADE, null = True, related_name='body_colour')
eye_colour = models.ForeignKey(EyeColour, on_delete=models.CASCADE, null = True, related_name='eye_colour')
user = models.ForeignKey(User, on_delete=models.CASCADE, null = True)
bio = models.TextField(null = True, blank = True)
born = models.DateField(auto_now_add=True)
image = models.ForeignKey(Image, on_delete=models.CASCADE, null = True, blank=True)
Adding rat.save() to the serializer gives me the following error: TypeError: int() argument must be a string, a bytes-like object or a real number, not 'dict'
I have a button that users can click on to add an object to their account. It's sent through django to my sqlite db, and in the console log there are no errors. It says "201 Created", and yet there is no new object in the database.
Someone else had the same problem as me, the solution was getting rid of some methods in the serializer, but they don't specify what they got rid off.
What's causing this?
Here is my serializer:
class RatSerializer(FlexFieldsModelSerializer, serializers.ModelSerializer):
name = serializers.CharField()
user = serializers.CharField(source='user.username', required=False)
userid = serializers.CharField(source='user.id', required=False)
body_colour = BodyColourSerializer()
eye_colour = EyeColourSerializer()
image = ImageSerializer(required=False)
class Meta:
model = rat
exclude = ['bio']
def create(self, data):
request = self.context.get("request")
user = request.user
return rat( name = data["name"],
body_colour = BodyColour(name=data["body_colour"]["name"]),
eye_colour = EyeColour(name=data["eye_colour"]["name"]),
image = Image(),
user = user)
My views:
class ratViewset(ModelViewSet, APIView):
serializer_class = RatSerializer
# queryset = rat.objects.all()
def get_queryset(self):
user = self.request.user
if user.is_anonymous:
return rat.objects.all()
return rat.objects.filter(user=user.id)
def post(self, request):
rat = request.data.get('rat')
# creates rat with the above data
serializer = RatSerializer(data=rat)
if serializer.is_valid(raise_exception=True):
serializer.save()
print(serializer.data, type(serializer.data))
return Response(serializer.data, status=status.HTTP_201_CREATED)
return Response(serializer.errors, status=status)
Grateful for any help!

How to simply get the BooleanField of a model which is True if and only one can be true at all in Django?

I have the following model which has two boolean fields. Due to my logic, they won't be both true at anytime. How can I get the field which is True in a simple straight forward way?
class Vote(models.Model):
poller = models.ForeignKey(Poller, on_delete=models.CASCADE, related_name='vote')
user = models.ForeignKey(Account, on_delete=models.CASCADE)
created_on = models.DateTimeField(auto_now_add=True)
poller_choice_one_vote = models.BooleanField(default=False)
poller_choice_two_vote = models.BooleanField(default=False)
def __str__(self):
return f'Vote by {self.user}'
This is how I'm doing it right now:
voted_for = Vote.objects.get(poller_id=poller_id, user=request.user)
is_true = voted_for.poller_choice_one_vote
is_also_true = voted_for.poller_choice_two_vote
if is_true:
voted_for = voted_for.poller_choice_one_vote
elif is_also_true:
voted_for = voted_for.poller_choice_two_vote
else:
pass
Something like this perhaps to use the meta to get the name of the field:
voted_for = Vote.objects.get(poller_id=poller_id, user=request.user)
one = voted_for.poller_choice_one_vote
two = voted_for.poller_choice_two_vote
if one:
voted_for = Vote._meta.get_field(one)
elif two:
voted_for = Vote._meta.get_field(two)

QuerySet' object has no attribute 'posts'

Am having a challenge to get users post on their timeline and I would appreciate if anyone could help me. I keep coming against this error(QuerySet' object has no attribute 'posts') when ever filter through the post object. I wonder what am missing out on here ? here is my model for post.
class Post(models.Model):
description = models.CharField(max_length=5000, blank=True)
pic = models.ImageField(upload_to='path/post/img' ,blank=True)
date_posted = models.DateTimeField(auto_now_add=True)
user_name = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
users = models.ManyToManyField(settings.AUTH_USER_MODEL, blank=True, related_name="users")
tags = models.CharField(max_length=100, blank=True)
here is my view for users account since i want every user to have their post on their timeline.
def account_view(request, *args, **kwargs):
"""
- Logic here is kind of tricky
is_self
is_friend
-1: NO_REQUEST_SENT
0: THEM_SENT_TO_YOU
1: YOU_SENT_TO_THEM
"""
context = {}
user_id = kwargs.get("user_id")
try:
account = User.objects.get(pk=user_id)
except:
return HttpResponse("Something went wrong.")
if account:
context['id'] = account.id
context['username'] = account.username
context['bio'] = account.bio
context['get_full_name'] = account.get_full_name
context['email'] = account.email
context['profile_pic'] = account.profile_pic.url
context['cover_image'] = account.cover_image.url
context['city'] = account.city
context['country'] = account.country
context['gender'] = account.gender
context['hide_email'] = account.hide_email
try:
post_list = Post.objects.filter(user_name=account)
except Post.DoesNotExist:
post_list = Post(user_name=account)
save.post_list()
posts = post_list.posts.all()
context['posts'] = posts
try:
friend_list = FriendList.objects.get(user=account)
except FriendList.DoesNotExist:
friend_list = FriendList(user=account)
friend_list.save()
friends = friend_list.friends.all()
context['friends'] = friends
You have to change to: posts = post_list.all()

Django changing models field into views

I everyone, I have a problem with a django's view. My goal is to change the 'execute' field into 'True' if newOrder is buy and there is some other sell order with a inferior price. And reverse for sell newOrders. I want to change the 'execute' field for the newOrder and also for the other order (in pairs). That's my code:
views.py
def order(request):
form = OrderForm()
if request.method == 'POST':
form = OrderForm(request.POST)
if form.is_valid():
new_order = form.save()
if new_order.buy is True:
sellOrder = Order.objects.filter(sell=True, execute=False,
price__lte=new_order.price).first().update(execute=True)
new_order.execute = True
sellOrder.save()
else:
buyOrder = Order.objects.filter(buy=True,
execute=False,price__gte=new_order.price).first().update(execute=True)
new_order.execute = True
buyOrder.save()
new_order.profile = request.user
new_order.save()
return redirect('home')
else:
form = OrderForm()
contex = {'form': form}
return render(request, 'app/new_order.html', contex)
models.py
class Profile(models.Model):
_id = ObjectIdField()
user = models.ForeignKey(User, on_delete=models.CASCADE)
wallet = models.FloatField()
class Order(models.Model):
_id = ObjectIdField()
profile = models.ForeignKey(User, on_delete=models.CASCADE)
datetime = models.DateTimeField(auto_now_add=True)
buy = models.BooleanField(blank=True)
sell = models.BooleanField(blank=True)
price = models.FloatField()
quantity = models.FloatField()
execute = models.BooleanField(blank=True)
But something goes wrong. This is the error:
AttributeError at /new_order/
'NoneType' object has no attribute 'update'
sellOrder returns a count of updated rows, not the object updated
sellOrder = Order.objects.filter(sell=True, execute=False,
price__lte=new_order.price).first().update(execute=True)
instead try
sellOrder = Order.objects.filter(sell=True, execute=False,
price__lte=new_order.price).first()
new_order.execute = True
sellOrder.execute = True
sellOrder.save()

How can I obtain just one JSON object with the three models information combined?

How can I pass the foreign key values from my model to my serialised json object?
Now I have this three models,
class Fleet(models.Model):
fleet_id = models.IntegerField('Id flota', primary_key=True, unique=True)
fleet_name = models.CharField('Nombre flota', max_length=20, unique=True)
def __str__(self):
return self.fleet_name + ' ' + str(self.fleet_id)
class Device(models.Model):
dev_eui = models.CharField(max_length=16, primary_key=True, unique=True)
producer = models.CharField(max_length=20)
model = models.CharField(max_length=20)
dev_name = models.CharField(max_length=20, unique=True)
fleet_id = models.ForeignKey(Fleet, on_delete=models.CASCADE)
def __str__(self):
return self.dev_eui
class DevData(models.Model):
data_uuid = models.UUIDField(primary_key=True, default=uuid.uuid1, editable=False)
frequency = models.IntegerField()
data_1 = models.FloatField()
data_2 = models.FloatField(null=True, blank=True)
dev_eui = models.ForeignKey(Device, on_delete=models.CASCADE) #hay que saber porque aƱade _id
def __str__(self):
return self.dev_eui
And what I'm doing is call my view function in my JS code to obtain some data like this.
def getData(request):
ctx = {}
if request.method == 'POST':
select = int(request.POST['Select'])
data = DevData.objects.order_by('dev_eui','-data_timestamp').distinct('dev_eui')
nodes = Device.objects.all()
fleets = Fleet.objects.all()
data = loads(serializers.serialize('json', data))
nodes = loads(serializers.serialize('json', nodes))
fleets = loads(serializers.serialize('json', fleets))
ctx = {'Data':data, 'Nodes':nodes, 'Fleets':fleets}
return JsonResponse(ctx)
And inside my js file I filter it with some if else conditionals.
This works well, but I'm sure I can do it directly in my view but I don't know how. How can I obtain just one JSON object with the three models information combined?
Thank you very much!!
You can write a custom serializer like this:
from django.core.serializers.json import Serializer
class CustomSerializer(Serializer):
def end_object(self, obj):
for field in self.selected_fields:
if field == 'pk':
continue
elif field in self._current.keys():
continue
else:
try:
if '__' in field:
fields = field.split('__')
value = obj
for f in fields:
value = getattr(value, f)
if value != obj and isinstance(value, JSON_ALLOWED_OBJECTS) or value == None:
self._current[field] = value
except AttributeError:
pass
super(CustomSerializer, self).end_object(obj)
Then use it like this
serializers = CustomSerializer()
queryset = DevData.objects.all()
data = serializers.serialize(queryset, fields=('data_uuid', 'dev_eui__dev_eui', 'dev_eui__fleet_id__fleet_name'))
I have wrote an article regarding serializing nested data here. You can check that out as well.