Just god
Can anyone help me?
Ngdream
Muflone
you talk like i don't have research
Your is a complex question involving a lot of things.
Learn Django and create every needed part to do that. If you have a concrete question ask your doubt
Ali
Hello guys
I wanna make a bidirectional relation between two class model in django
For example:
A foreign key which shows the author
And a many to many which shows all books of an author
Is this design correct?
Анатолий
Hello guys
I wanna make a bidirectional relation between two class model in django
For example:
A foreign key which shows the author
And a many to many which shows all books of an author
Is this design correct?
You can simply use foreign key for author in the book model.
And if you need, for example, to get all the books of the author AUTHOR_NAME, you can use ModelName.objects.filter():
author_books = Book.objects.filter(author=AUTHOR_NAME)
Or, if a book may have several authors, use ManyToMany field (inside one of the models. It depends on your needs where you should to add the field – into Author model or Book model. But technicaly, I think, there's no a lot of difference)
If you perform ManyToMany field, you also will be able to get a value of the outer model
An example:
- We add ManyToMany field into Author model.
...
books = models.ManyToMany(to=Book)
...
- And whenever you need to get an author of a book, you can simply use the lowercased name of the author model. In this case Book model will have automatically a field "author" which is containing an id of the author whom the book belongs to.
books_by_AuthorName = Book.objects.filter(author=AuthorName)
(Notice, that we haven't define "author" field in Book model, it refers to an object of Author model)
(Maybe I made some little mistakes in my description, take a look at the errors you get in the process, they are really helpful and sometimes tell you about your models more than you can imagine 😅)
Anonymous
If i use User.objects.get() is it safe?
if the username is not there in the User, then it will return an error, So better use User.objects.filter(), then check whather the object is exists with with exists() function
Ali
You can simply use foreign key for author in the book model.
And if you need, for example, to get all the books of the author AUTHOR_NAME, you can use ModelName.objects.filter():
author_books = Book.objects.filter(author=AUTHOR_NAME)
Or, if a book may have several authors, use ManyToMany field (inside one of the models. It depends on your needs where you should to add the field – into Author model or Book model. But technicaly, I think, there's no a lot of difference)
If you perform ManyToMany field, you also will be able to get a value of the outer model
An example:
- We add ManyToMany field into Author model.
...
books = models.ManyToMany(to=Book)
...
- And whenever you need to get an author of a book, you can simply use the lowercased name of the author model. In this case Book model will have automatically a field "author" which is containing an id of the author whom the book belongs to.
books_by_AuthorName = Book.objects.filter(author=AuthorName)
(Notice, that we haven't define "author" field in Book model, it refers to an object of Author model)
(Maybe I made some little mistakes in my description, take a look at the errors you get in the process, they are really helpful and sometimes tell you about your models more than you can imagine 😅)
Thank you
I understand it completely
I didn't know how is possible to get reverse relation and I wanted to make It my self
KingFx
guys! i implementing authentification process with django I want to give acces to certain ressources in a web app , but users should have an opening session I want to know how I can redirect users to the prevous url after login
Ravikumar
how to add add extra field to the ContentType model django
Artin
Anonymous
Can I use Django forms to create drf serializers?
Charly
Charly
DRF has its own serializers
TonFutures 💎
Hi
Can anyone help me?
TonFutures 💎
from django.contrib.auth.models import User
from django.contrib.auth.backends import BaseBackend
class CustomBackend(BaseBackend) :
def authenticate(self, request, username = None) :
try :
user = User.objects.get(username = username)
return user
except User.DoesNotExist :
return None
setting.py :
AUTHENTICATION_BACKENDS = [
'd1.custom_backend.CustomBackend',
'django.contrib.auth.backends.BaseBackend'
]
TonFutures 💎
I create custom authenticate
TonFutures 💎
from django.contrib.auth import login, authenticate
def SignIn(request) :
user = authenticate('gg')
print(user)
form = MyForm()
return render(request, 'login.html', {'form' : form})
TonFutures 💎
But when i print user
It returns None
TonFutures 💎
'gg' is present in my auth_user table, in field username
Caleb
TonFutures 💎
TonFutures 💎
TonFutures 💎
AUTHENTICATION_BACKENDS = [
    'd1.custom_backend.CustomBackend',
    'django.contrib.auth.backends.BaseBackend'
]
TonFutures 💎
It work, i'm sure
Because i set print(1) in authenticate and it work
TonFutures 💎
from django.contrib.auth.models import User
from django.contrib.auth.backends import BaseBackend
class CustomBackend(BaseBackend) :
    def authenticate(self, request, username = None) :
print(1)
        try :
            user = User.objects.get(username = username)
            return user
        except User.DoesNotExist :
            return None
Caleb
Charly
Zhaks
Does .first() method in Django ORM hits db?
Charly
Ayaz
Can anyone help me to run my project giving some error?
Charly
Ayaz
uquacore
If I have a django all-auth module that is responsible for signing a user up via GitHub in my Django project, am I supposed to change my basic registration and login to all-auth ones instead of the standard Django or isn't it necessary?
Dlight
uquacore
uquacore
It seems to me I will have to change everything I have for now because of social auth I am trying to make
Kumar Shreyansh
Hi all, what is the best way to do cache management in django? My application keeps malfunctioning unless cache is cleared every time
Koen de
Steve
Please help my issue :
Collecting mysqlclient
Using cached mysqlclient-2.2.0.tar.gz (89 kB)
Installing build dependencies ... done
Getting requirements to build wheel ... error
error: subprocess-exited-with-error
Koen de
Muflone
Dhurke Sahdmadhi
शिवांजली
ilham
how to create a form with fields from different model?
Targoon
but seriously, why people do not seaech in google
Ohama |
Is there plugin validator based on text similarity?
Максим
DRF
class Book(Model):
    name = CharField(max_length=15, unique=True)
class BookSerializer(ModelSerializer):
    class Meta:
        model = Book
        fields = ('name',)
class AuthorSerializer(ModelSerializer):
    book = BookSerializer() # need validation of json when create
    class Meta:
        model = Author
        fields = ('id', 'book')
How to disable unique validation of the field "name" in AuthorSerializer, in nested BookSerializer?
I do not want to change model Book or BookSerializer.
What I do:
I try to create an Author with get_or_create Book. If I use get_or_create, I do not need unique validation
Muxtor
Максим
C
Hi all. I’m just starting out to learn Django. But I’m not understanding it. Are there any resources to revise my python. And also any resources to help a beginner Django learning. General advice on resources, books, etc that will be helpful. If there’s anyone here who would like to mentor me and give me what o study each week, and whom I can report progress to, I will be Glad.
T
Hello, I have my app "base" so the admin url is localhost/admin/base .. i dont need the page localhost/admin that list all the apps inside my project.. How can I do to make the user reach admin/base instead /admin after login as admin at the url localhost/admin? Thanks a lot.
Anonymous
pls how can i fix this error my code link is https://linkode.org/#9b5wxCSbiWnr2f0Iw05xn
Anonymous
error message is this ImproperlyConfigured at /companies/
Could not resolve URL for hyperlinked relationship using view name "campany-details". You may have failed to include the related model in your API, or incorrectly configured the lookup_field attribute on this field.
Anonymous
🙏 i need the help what am doing is to create user from my model
Anonymous
using some of the fields in my model
Pradheep
from .models import Customer, Product, Order
ImportError: attempted relative import with no known parent package
i am getting this error
could some help me to fix this
cj
Doragonsureiyā
Kumar Shreyansh
TonFutures 💎
Hi
What is the most reasonable way to create a signup page?
Ohama |
Hi, anyone know how to redirect a view base on function that required permission/authorized authentication to redirect to non login view.? I have redirect with decoreator in setting but it add /next url
Kumar Shreyansh
Ohama |
Kumar Shreyansh
Kumar Shreyansh
In def login(request), put return redirect above url format. Put app_name='some name' in urls.py file above urlpatterns in your django app urls.py file, not in global urls.py file
Kumar Shreyansh
Kumar Shreyansh
Please share views.py, urls.py files
Ohama |
Please share your code via github link or pasting service
There is no detail need to know about it because i just add permission to root url when clients visit the root url i want to redict to non login view then django always add /next parameters. Let say my non login view is contact info.
Kumar Shreyansh
Kumar Shreyansh
Demsy
I want to display a message using
Message.info
But I get this error
SafeString object has no attributes 'info'