ForeignKey vs OneToOne field django [duplicate]

2024/10/6 10:31:29

I need to extend django user with some additional fields . I found 2 different ways there

class UserProfile(models.Model):user = models.OneToOneField(User)#other fields

OR

class UserProfile(models.Model):user = models.ForeignKey(User)#other fields

Aren't they same? After syncing them, i saw no difference in mysql database

Answer

No, why would you think that? A ForeignKey is a one-to-many relationship - ie a user could have many profiles. A OneToOne is, as the name implies, a one-to-one relationship - a user can only have one profile, which sounds more likely.

https://en.xdnf.cn/q/70379.html

Related Q&A

How to sort glob.glob numerically?

I have a bunch of files sorted numerically on a folder, when I try to sort glob.glob I never get the files in the right order.file examples and expected output sorting folder ------ C:\Users\user\Deskt…

How to determine a numpy-array reshape strategy

For a python project I often find myself reshaping and re-arranging n-dimensional numpy arrays. However, I have a hard time to determine how to approach the problem, visualize the outcome of the result…

matplotlib plotting multiple lines in 3D

I am trying to plot multiple lines in a 3D plot using matplotlib. I have 6 datasets with x and y values. What Ive tried so far was, to give each point in the data sets a z-value. So all points in data …

How to get a telegram private channel id with telethon

Hi cant figure out how to solve this problem, so any help will be really appreciated. Im subscribed to a private channel. This channel has no username and I dont have the invite link (the admin just ad…

boolean mask in pandas panel

i am having some trouble masking a panel in the same way that I would a DataFrame. What I want to do feels simple, but I have not found a way looking at the docs and online forums. I have a simple ex…

How can I move the text label of a radiobutton below the button in Python Tkinter?

Im wondering if theres a way to move the label text of a radiobutton to a different area, e.g. below the actual button.Below is an example of a few radiobuttons being placed using grid that Im using:fr…

play sound file in PyQt

Ive developed a software in PyQt which plays sound.Im using Phonon Library to play the sound but it has some lag.So how can I play a sound file in PyQt without using Phonon Library.This is how I am cur…

translating named list vectors from R into rpy2 in Python?

What is the equivalent of the following R code in Rpy2 in python?Var1 = c("navy", "darkgreen") names(Var1) = c("Class1", "Class2") ann_colors = list(Var1 = Var1…

Issue parsing multiline JSON file using Python

I am trying to parse a JSON multiline file using json library in Python 2.7. A simplified sample file is given below:{ "observations": {"notice": [{"copyright": "Copy…

timezone aware vs. timezone naive in python

I am working with datetime objects in python. I have a function that takes a time and finds the different between that time and now. def function(past_time):now = datetime.now()diff = now - past_timeWh…