Kori
Hey I'm new to django and I'm really stuck when running a migration I get UNIQUE key constraint failed on the onetoonefield connected to user Could anyone please help me
Kori
I make the migrations it's ok When I do the migrate that's when I get the error
inchidi
may i see your last migration file?
Kori
Ok lemme send you
inchidi
use dpaste.de or pastebin btw
Maz
what are some interesting Advanced Django topics i would want to take a look at?
inchidi
what are some interesting Advanced Django topics i would want to take a look at?
creating rest/graphql api with drf or graphene, asynchronous tasks with celery, sync and async websocket with django channel
Maz
Also, if i need to show the time an object was deleted from the db, how would i go about doing it?
Maz
e.g. a Patient model....how do i display when a patient record is deleted?
Kori
https://pastebin.com/AfXPauWq
inchidi
e.g. a Patient model....how do i display when a patient record is deleted?
there's so many way to do that, the most simple way is creating PatientLog model and store it there on delete signal
inchidi
https://pastebin.com/AfXPauWq
you can do something like this https://dpaste.de/FDMp
inchidi
providing default value for your Employees.user field
inchidi
🤔 interesting....tell me more..
maybe try it first, creating the log model, and on delete signal of the model then tell us when you stuck
Maz
okay,
inchidi
you will realize the cons of that approach on your journey
inchidi
what error produced?
Kori
Same error IntegrityError: UNIQUE constraint failed
inchidi
ah so you have so many employees data
inchidi
so, what user do you want for each employees data you have?
inchidi
any specific rules?
Kori
when a user registers for the site I want them to fill in additional details about themselves using a model form.
Kori
I'm not sure if that's the correct approach I'm new to the framework
inchidi
you must specify the user field for each Emplyees object first before alter it with OneToOne then
inchidi
https://pastebin.com/AfXPauWq
you must delete this migration file first and make Employees.user = ForeignKey(User, ...) first instead OneToOne
Kori
I'm not sure if that's the correct approach I'm new to the framework
I'll try that Thank you very much Will let you know if I run into problems 🙂
inchidi
when a user registers for the site I want them to fill in additional details about themselves using a model form.
OneToOne is the correct relationship in this case. but you already have Employees data with no relationship to User. when you alter it to OneToOne it throw an error so you need to specify each user for each Emplyees data you have first by using ForeignKey (because FK not unique so migration will not throw an error) , then you can make it OneToOne
Kori
Oooh I didn't know that Thanks Cause it was still throwing me errors 😂
Kori
Thanks a lot So should I first delete the model and the migrations and restructure it again?
inchidi
https://pastebin.com/AfXPauWq
you just need to revert migration of user = OneToOne() by deleting this migration file
inchidi
you dont need to delete the model, just make user=ForeignKey(User, on_delete-models.CASCADE) on class Employees
Kori
It's worked Thank you
Maz
https://pastebin.com/xqhXqh1H
Maz
https://pastebin.com/mFEqzZg8
Maz
how can i list all the next of kin for a particular patient? e.g. a patient with id '2'...i should be able to enter this id in the url and see a list of related kin for the patient
Mirco
how can i list all the next of kin for a particular patient? e.g. a patient with id '2'...i should be able to enter this id in the url and see a list of related kin for the patient
https://docs.djangoproject.com/en/2.0/ref/class-based-views/generic-display/#django.views.generic.detail.DetailView have a look here
Django Bot
>> Links - How to Structure Django Projects
N S
Is it possible to set default values of a field in the createview or inside the template where the createview form is rendered without using modelforms
N S
and how to set it in templates
inchidi
simply {{ context_name }}
N S
fine
Pepeluqui
Hi, Any way to generate an executable of a django application?
Pepeluqui
I have tried with pyinstaller without luck
inchidi
Hi, Any way to generate an executable of a django application?
what is the reason? why you want to make your web app become .exe?
Pepeluqui
To make it portable to other users
inchidi
you can simply create .bat (windows) or .sh file for that?
Pepeluqui
Yes but I also would like to ofuscate the code
inchidi
but your user could access the db directly instead if they want tho
N S
how to upload csv file to models using modelform
inchidi
add enctype="multipart/form-data" to the <form>. read also
inchidi
I have tried with pyinstaller without luck
what is the error? already read this?
Pepeluqui
I have followed those steps
Pepeluqui
Let me give you the exactly screenshot in a while. I am not with the computer now.
Django Bot
>> Links - Building a todo app using Vue.js and Django as the backend : vuejs - Django Stripe Tutorial - TestDriven.io
Django Bot
>> Links - Cross Site Request Forgery protection | Django documentation | Django - Django Channels Tutorial - Building Progress Bars for the Web with Django and Celery | Build with Djan - NewbieMistakes – Django
Pepeluqui
This is the error before creating an executable with pyinstaller
Django Bot
>> Links - python - Run django application without django.contrib.admin - Stack Overfl
Django Bot
>> Links - CS50's Web Programming with Python and JavaScript - Try DJANGO Tutorial Series - YouTube
Karim Norman
I have a questions, how I can create a queryset where the foreignkey is filtered? An example. I have a model where Postcode is a model object and has a one to many relation with suburb with the rel name suburbs. Now if I create a queryset postcodes= Postcode.objects.filter(suburbs__name__startswith=SEARCH_TERM) I get the all the postcodes that includes the suburbs starting with the search term. But the postcodes can include also other suburbs and I would like to construct the queryset to have JUST the suburbs in the serch term. Is there any way to do that?
Code9
suburbs__name = SEARCH_TERM would work
Karim Norman
but I need the startswith, because the search term can be just few letters
inchidi
what do you mean by "the search term can be just few letters" btw?
Code9
I would like to construct the queryset to have JUST the suburbs in the serch term
Code9
Do you mean you would like to check if the format for the postcode is correct first
Code9
if len(SEARCH_TERM) > 5: postcodes= Postcode.objects.filter(suburbs__name__startswith=SEARCH_TERM) else: postcodes= Postcode.objects.filter(suburbs__name=SEARCH_TERM)
Karim Norman
what do you mean by "the search term can be just few letters" btw?
I'm building the filter for and endpoint therefore I don't need the complete suburb term, it could be just the initial (e.g. Syd would match `Sydney`)
inchidi
suburbs__name = SEARCH_TERM would work
then this would work for sure
inchidi
suburbs__name = SEARCH_TERM different with suburbs__name_iexact = SEARCH_TERM
Karim Norman
I used startswith
Code9
I used startswith
would syd work if you want Sydney?