Allan
The models are entries that each one should belong to a user. Let's say for example a Journal: each entry in the journal table should belong and accessed by only the owner. But this user thing wasn't planned and now I'm trying to fix this. I'm good at python, but I am not experienced in Django.
Allan
The first approach is to have a different database for each user. But there are some models (tables) that are general and not user specific, and after the user log in he need to use there (ie. I cannot run a different instance of Django for each user). This one I believe is pretty impractical with Django.
Allan
The other one is using an internal user field. So, as all models that are user specific user a Custom Abstract Base model to save creation and update times, and no one else uses it, It’s already on the way.
Allan
I override the save function on this Base class to always take the user (with a few hacks) and save it. And created a custom manager that filter the results for the ones the user owns. I’m having a lot of trouble with this approach, I don't now if it's the best one.
Allan
What I would like to now is if this is the best approach, if there is any realistic possible approach besides those, what would be the better ones. Or even if I’m missing something like a django module that makes this, or anything.
Elizaveta
Hey, can anybody help please. I'm making models for my db. The First model is dish, it has name and price. And the second one is order, it should have 4 fields, two of which are 'total price' and 'consists of'. These fields are foreign keys from dish and may have any number of elements. How do i properly bound them in model?
Anonymous
I don't understand what you're trying to say
Anonymous
Website
Anonymous
?
Anonymous
http://graphql.org/learn/
Anonymous
It's is python version GraphQL
Elizaveta
I don't understand what you're trying to say
I meant that i need to make one-to-many field (?)
Elizaveta
I don't know how to do it
Elizaveta
The first field will have several values from another class. Is it: value = models.ForeignKey('another class') ?
Elizaveta
But what if i want to store several values there? How i can do it?
Abhi
@Formalin extend the user model
Abhi
and make a one-to-one relationship with that extended user model
Elizaveta
Could you please show me an example?
Abhi
Okay wait
Abhi
https://simpleisbetterthancomplex.com/tutorial/2016/07/22/how-to-extend-django-user-model.html
Abhi
hope it help
inchidi
The first approach is to have a different database for each user. But there are some models (tables) that are general and not user specific, and after the user log in he need to use there (ie. I cannot run a different instance of Django for each user). This one I believe is pretty impractical with Django.
you don't need different database for each user, you can use models.ForeignKey(User) if you are using an internal user field (looks like you are extending django User) then you cant store multiple Journal for each user, arent you? if i understand you correctly i think the best approach for your situation is create Journal model with user field as foreignkey models.ForeignKey(User) » https://docs.djangoproject.com/en/1.11/ref/models/fields/#foreignkey and then using django signals to make your user always have at least one Journal data https://docs.djangoproject.com/en/1.11/ref/signals/#signals
inchidi
so for every order you will store the dishes ordered, and how many is it. your views gonna do the math and you get the bills
inchidi
It's is python version GraphQL
GraphQL sounds cool, its like another rest api, isnt it? i dont know yet when i will need it 😕
Django Bot
>> Blogs - Is Django the Right Fit for your Project?
Anonymous
GraphQL sounds cool, its like another rest api, isnt it? i dont know yet when i will need it 😕
Data fields and structure is defined by front-end, not the back-end
Anonymous
But it have disadvantage that the frontend man have to communicate many time with backend man
Anonymous
Unless you're full stuck
inchidi
i see, sounds like its gonna give flexibility for front end
Anonymous
so it will split data for each model?
Most like all models and it's fields was collected in a class
Anonymous
Then frontend man pick what his wanted
inchidi
Then frontend man pick what his wanted
nice, btw thanks for the link http://graphql.org/learn/ 👍
Anonymous
You are welcome
Anonymous
Frontend developer can do whatever he can
Anonymous
Then back end developer ask him to change something
Anonymous
Agree with you
inchidi
Frontend developer can do whatever he can
i think thats what @ToonoW said plus, the communication between frontend and backend is getting bigger
Anonymous
I thought he said opposite
Anonymous
Anyway
Anonymous
If Meaning same, no problem
Anonymous
Allan
you don't need different database for each user, you can use models.ForeignKey(User) if you are using an internal user field (looks like you are extending django User) then you cant store multiple Journal for each user, arent you? if i understand you correctly i think the best approach for your situation is create Journal model with user field as foreignkey models.ForeignKey(User) » https://docs.djangoproject.com/en/1.11/ref/models/fields/#foreignkey and then using django signals to make your user always have at least one Journal data https://docs.djangoproject.com/en/1.11/ref/signals/#signals
Yeah. the part of saving the correct used to every record is OK I Think, it's been working so far. All models that should be used specific extend a Custom Abstract Model class in the project instead models.Model directly. So I just created the user field there as a foreign key to the user. And I override save method to setup the user before saving. To figure out the user, it can be passed as argument to the save function ( like I do in Django shell) or it search for a request object in the local vars of the call stack, and after it finds its, take the request.user as the logged user. It's hasn't broke anything (yet).
Allan
The main problem is always filtering the query to just return the records that the user own. I extended the default Manager for this custom class, so the get_queryset always filter by the current user. It works fine on the shell, and on the views too I think.
Allan
But when I start the server it crashes because the ModelForm does a query when creating the class or instance at the initialization. Because it doesn't pass any user, and there is no user related to that query, I can't handle it.
Allan
Because there's no sense for me making queries on a model without even a user been logged in.
Allan
It also breaks many others stuffs that I have no idea why.
Allan
That's why I'm questioning about if this is the right approach, because I would have to know in detail all Django source to know the answer for this, or how to fix stuffs.
Django Bot
>> Blogs - Two Scoops of Django Birthday Giveaway
Anonymous
Congratulation....got 90 members....👍
Anonymous
Anybody here wanna do for a gamble game website ?
Lapanit ☃️
How much are you paying?
Anonymous
Val
what's funny?
inchidi
That's why I'm questioning about if this is the right approach, because I would have to know in detail all Django source to know the answer for this, or how to fix stuffs.
i think am understand what you want to do, and i think what you really need is django signals. instead extending default Manager, using django signals when user logged in and specify which model/data/other you will use easier. https://stackoverflow.com/a/6109366/3925477 but still its just my opinion
Allan
My problem now is that the ModelForm populate the ForeginFields at startup, and then doesn't update (make the query again), so it only got results for this fileds of the first user...
inchidi
if user will not input that fields, you can just exclude it for that ModelForm
inchidi
then your views pass the request.user
Django Bot
>> Jobs - 2017 - 2018 Fall & Winter Django Web Developer Internship >> Blogs - Find static files defined in django-pipeline but not found
Lapanit ☃️
/warn ad
Group Butler
Hacker Linux™ has been warned (1/3)
Val
hello does any one have an article or book on recursion in python
Anonymous
Book on recursion?
Anonymous
That's too much
Val
i found something thanks
Val
class Test(unittest.TestCase): def test_should_say_hello(self): self.assertEqual(say_hello("NAME"), "Hello, NAME!")
Val
pls can someone explain this codes to me?
Bagdat
class Test(unittest.TestCase): def test_should_say_hello(self): self.assertEqual(say_hello("NAME"), "Hello, NAME!")
You comparing result of function say_hello() with expecting string. If result and expect string are equal test will be success. Of not will be raise exception
Val
For this practice challenge, write a simple greeter function. If it is passed in a name NAME, return 'Hello, NAME!. If it is called without a name, return 'Hello there!.
Val
class Test(unittest.TestCase): def test_should_say_hello(self): self.assertEqual(say_hello("Qualified"), "Hello, Qualified!")
Val
def say_hello(name):