Sam
Sam
This is my model:
class Course(models.Model):
title = models.CharField(max_length=75, unique=True)
class Requirements(models.Model):
course = models.ForeignKey(Course, on_delete=models.CASCADE, related_name="course_requirements")
title = models.CharField(max_length=75)
now my question is how can i update or create a requirement instance for a post through postman or api ?
Auc
Sam
Call the field in your views files and pass in whatever u need
this is my serialization method:
def update(self, request, pk=None):
instance = get_object_or_404(Course, id=pk)
serializer = serializers.CourseSerializer(data=request.data, partial=True)
if serializer.is_valid():
serializer.update(instance=instance, validated_data=serializer.validated_data)
return Response({"response": "Your Course has been updated successfully"}, status=status.HTTP_200_OK)
else:
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
i don`t know what should i write in the postman`s field !
Auc
I can't see your api view
Sam
im using view set
class CourseViewSet(ViewSet):
Auc
Like your http method is it get , post , delete or update u just declared a function
Sam
Sam
look in my models i have 2 model, first one is Course and second one is Requirements
Requirements model is related to course model thorugh a foreignkey
Sam
then imagine i have this url:
http://127.0.0.1:8000/courses/1/
which i retrieve a course, notice just a course
Sam
now i want to edit or create a requirement instance through this url
Sam
is it possible ?
Akintola
Yes it is
Akintola
Use the foreign key to get the object then you assign (update) to the new value
Sam
Is this method appropriate?
def put(self, request, course_id, requirement_id):
try:
course = Course.objects.get(pk=course_id)
requirement = Requirements.objects.get(pk=requirement_id, course=course)
title = request.data.get('title')
requirement.title = title
requirement.save()
return Response({'message': 'Requirement updated successfully'}, status=status.HTTP_200_OK)
except Course.DoesNotExist:
return Response({'message': 'Course not found'}, status=status.HTTP_404_NOT_FOUND)
except Requirements.DoesNotExist:
return Response({'message': 'Requirement not found for this course'}, status=status.HTTP_404_NOT_FOUND)
Akintola
Sam
I think you should separate the update logic in views
Like this?
class UpdateRequirement(APIView):
def put(self, request, course_id, requirement_id):
try:
course = Course.objects.get(pk=course_id)
requirement = Requirements.objects.get(pk=requirement_id, course=course)
title = request.data.get('title')
requirement.title = title
requirement.save()
return Response({'message': 'Requirement updated successfully'}, status=status.HTTP_200_OK)
except Course.DoesNotExist:
return Response({'message': 'Course not found'}, status=status.HTTP_404_NOT_FOUND)
except Requirements.DoesNotExist:
return Response({'message': 'Requirement not found for this course'}, status=status.HTTP_404_NOT_FOUND)
Akintola
Yes
Akintola
Something like that
Sam
Sam
I have another question bro
Sam
If you check the amazon`s product detail, you`ll found an html table with 2 columns, how can i implement that with Django?
Sam
I mean how they order the left side columns ? and how they implement every table special for each category
The Inspired
The Inspired
and the layout is done in a JS framework like say next or nuxt
Deleted
How does one deal with API keys for a Django Rest Framework API
The Inspired
are you trying to create an API key for your own DRF API?
Deleted
Sam
The Inspired
what do you mean?
The Inspired
I don't understand
Sam
So the left column is for the specification, for example the brands name and the right column is for the value
Sam
Forexample graphic cart rtx 4090
Brand: Nvidia
Vram: 16gB
Power usage: 20W
The Inspired
well, its simple... you first decide what all you want in your model... then define the fields for it.
For example, in this case, the properties are brand, vram and power_usage and my product is a GPU.
so I can create a model called 'GPU' and define some properties in it... something like this:-
class GPU(Model):
name = models.CharacterField(max_length=100)
brand = models.CharacterField(max_length=100)
vram = models.IntegerField()
power_usage = models.IntegerField()
Mr Komilov
The Inspired
my bad. its not used for integerfield
Ibrohim
Hello everyone! I have a question: how to make login/register like X(twitter). What I'm thinking of doing (but haven't done yet): I'm thinking to make a custom user and combine with djnago-allauth(having this package and using it is a must for me). But I can't think of the following things: How to combine them, how to do: like a user logs in via socialmedia thanks to allauth and he needs to define a slug, date births (maybe it can be done with allauth package). How to do it in views using CBV and how to display in templates. Correct me if I'm thinking something wrong.
Nat
Hello guys,
Is there native way we can use faker for Django Geo Point field?
Kaustab Roy_C_24_CSE(AIML)
Hello guys,
I am having a question in my mind, please help me through this...
Using Django for backend should I use SQL query commands or should I use ORM. Or when should I use between SQL query and Django ORM?
The Inspired
Kaustab Roy_C_24_CSE(AIML)
The Inspired
lets say from a list of names, you want only those names which start with "kaustab".
This is a simple query, so you would do
Name.objects.filter(firstname__startswith="Kaustab")
now for a complex query, lets say you want only the last 25% names from the same list. You would have to use a compex query expression for that
something like Q
Poll.objects.get(
Q(question__startswith="Who"),
Q(pub_date=date(2005, 5, 2)) | Q(pub_date=date(2005, 5, 6)),
)
https://docs.djangoproject.com/en/5.0/topics/db/queries/
Adam
Hi, I have a issue with django channels
first I tried to upload the project on lightsail instance without enabled ssl
and the socket was connected successfully
but when I enabled ssl, the socket request still pending for a minute and then finished
Rêveur
Hello, I'm thinking to learn django where to find the good resources which will give me the core concepts and solid foundations about how django works and other stuff's i should know
.
Adam
Doragonsureiyā
Python groups in other languages
(⚠️Not managed by us ‼️)
Amharic @python_amharic
Arabic @arabipython
Azerbaijani @azepug
Chinese @pythonzh
French @python_francais
German @pythonde
Hebrew @pythonisrael
Hindi @pyhindi
Tamil @pythontamilcommunity
Indonesian @pythonid
Italian @python_ita @pythonita
Kazakh @python_kz
Malaysia @pythonmalaysia
Persian @pyfarsi
Portuguese @pythonbr
Russian @ru_python_beginners @ru_python @python_scripts
Spanish @PythonEsp
Swahili @python_eafrica
Turkish @python_tr
Uzbek @python_uz
If you have an active and well managed group that you want to be listed there, ask in the Meta group, link in the rules page.
Kaustab Roy_C_24_CSE(AIML)
pranav
Anyone who has used dj-rest-aurh + simple-jwt for authentication api in backend and react in frontend?
I want to know about how you have handled access and refresh token , by defining url from simple_jwt?
You're constantly getting refresh token from simple-jwt after access token expires?
I'm kind of confused, anyone help?
pranav
dj-rest-auth is handling all the urls regarding authentication e.g. login , logout , verify email and everything but what about access tokens if I have set them to expire after 5 mins do I need to get it by defining a simple-jwt endpoint or something?
Kingamir86
Hello everyone one I have problem
I have exist website created by django and postgresql
I want to extract auth in new app as SSO sinc I want to add more platforms with one login but in my db I have 7000 user data and I didn't have any idea how can I do this sinc I have lot s data related to user field and I can't lost them
Kingamir86
Please help me and guide me I'm in bad situation 🙏🏻
Charly
Kingamir86
So it should sync with sso
Kingamir86
It need to change user model and put data from sso
Naphtal
Kingamir86
Kingamir86
Thanks guys I should search and try to find way
Abbos
Hello everyone!
I have obtained digitalOcean server and put my project on it. When I run it, I get this error. how can I handle the error
django.db.utils.OperationalError: connection failed: FATAL: password authentication failed for user "root"
connection to server at "127.0.0.1", port 5432 failed: FATAL: password authentication failed for user "root"
Bekzod
Kanji
Can we able to make the form builder in django for dynamic form
Kanji
Fields
Abid
To work in Django, i need to know some things
Charly
Toto Syrax
[10/Jun/2024 07:01:03] "GET / HTTP/1.1" 200 139
[10/Jun/2024 07:01:03] "GET /static/css/main.css HTTP/1.1" 404 1795
[10/Jun/2024 07:01:03] "GET /static/images/cat.jpg HTTP/1.1" 404 1801
Toto Syrax
Toto Syrax
🏅♐Mark
can anyone help me. cant load css and images
Double check the path is correct or not
And if you create a css folder and that folder inside the static folder you need to say the path like this
"{% static css/'mycss.css' %}"
Say {% load static %}
In your html page that you want css
Toto Syrax
🏅♐Mark