Re: AttributeError: object has no attribute 'user' while trying to access instance.user.id

2020-09-15 Thread Mislav Jurić
Thank you for your suggestion. I will use the username, as it is unique as 
well.

Best,
Mislav

Dana ponedjeljak, 14. rujna 2020. u 19:45:36 UTC+2 korisnik coolguy napisao 
je:

> If i had to stick to your code then i would save the file with 
> instance.username. username is available and folder would be readable as 
> well.
>
> However, as i mentioned in my last reply and seems you agreed, 
> save/register the employee and then have employee upload the picture.
>
> On Monday, September 14, 2020 at 8:20:21 AM UTC-4 mislav@gmail.com 
> wrote:
>
>> What I can do is first register the employee via a register form, then 
>> once he logs in ask him/her for the profile picture. *Can I do that this 
>> way? *
>>
>> If I do it this way, I don't have to change my model in any way.
>>
>> Dana nedjelja, 13. rujna 2020. u 18:11:18 UTC+2 korisnik coolguy napisao 
>> je:
>>
>>> not sure about the purpose of showing that example in Django 
>>> documentation while its comments are clear that "object will not have been 
>>> saved to the database yet, so if it uses the default AutoField, *it 
>>> might not yet have a value for its primary key field*."
>>>
>>> so that's the reason for having None with file path.
>>>
>>> In this approach what i can see is to save the employee first without 
>>> file and then edit and select the file.
>>>
>>> I would not use this approach and rather keep it simple as followed:
>>>
>>> photo = models.ImageField(upload_to='employee/%Y/%m/%d/', blank=True)
>>>
>>> On Sunday, September 13, 2020 at 5:41:18 AM UTC-4 mislav@gmail.com 
>>> wrote:
>>>
 Hey coolguy,

 thanks for responding. After I changed that line as you suggested that 
 error is solved, *but when I add the user through the admin interface, 
 I get None as the ID* (the folder that gets created in the 
 */media/users* is titled *None*). I'm not sure if this is expected 
 behavior.

 I haven't added the registration or the login yet, so maybe the ID gets 
 a value when someone is actually registering.

 Let me know.

 Best,
 Mislav

 Dana subota, 12. rujna 2020. u 21:57:21 UTC+2 korisnik coolguy napisao 
 je:

> I wanted to see your model to understand but i realized after my last 
> post that you are using "instance.user.id" while your employee 
> instance does not have user field. (had to go somewhere urgently) >>> 
> return "employees/media/users/{0}/profile_picture.{1}".format(
> instance.user.id, extension)  
>
> change it as follow and remove user from it.
> return "employees/media/users/{0}/profile_picture.{1}".format(
> instance.id , extension)  
>
> It should work...
>
> On Saturday, September 12, 2020 at 3:17:28 PM UTC-4 
> mislav@gmail.com wrote:
>
>> coolguy here is the complete Employee model:
>>
>> class Employee(models.Model): #TODO: Double-check this
>> username = models.CharField(max_length=50, unique=True)
>> email = models.EmailField()
>> password = models.CharField(max_length=50)
>> first_name = models.CharField(max_length=150)
>> last_name = models.CharField(max_length=100)
>> website = models.URLField(max_length=200, blank=True)
>>
>> profile_picture = models.ImageField(upload_to=get_upload_path, 
>> blank=True, null=True)
>> 
>> def __str__(self):
>> return str(self.first_name) + str(self.last_name)
>>
>> *Why do I need the foreign key to User in the first place?* I don't 
>> recall seing the foreign key to User in any one of the tutorials.
>> Dana subota, 12. rujna 2020. u 20:20:11 UTC+2 korisnik coolguy 
>> napisao je:
>>
>>> Please share the complete employee model. It seems you are missing 
>>> User foreign key in it.
>>>
>>> On Saturday, September 12, 2020 at 2:03:20 PM UTC-4 
>>> mislav@gmail.com wrote:
>>>
 Hey guys,

 I have the following code in models.py file in one of my apps:

 def get_upload_path(instance, filename):
 extension = filename.split('.')[-1]
 return "employees/media/users/{0}/profile_picture.{1}".format(
 instance.user.id, extension)

 class Employee(models.Model):
 # some attributes here
 profile_picture = models.ImageField(upload_to=get_upload_path, 
 blank=True, null=True)

 I am getting the following error when I try to add an Employee via 
 the admin interface:

 AttributeError at /admin/employees/employee/add/

 'Employee' object has no attribute 'user'

 *I don't know where this error is stemming from.* I took the 
 get_upload_path function from the official Django FIleField 
 documentation 
 

Re: I've got two apps in my project and each one should have different authentication model. How do achieve this?

2020-09-15 Thread Mislav Jurić
Hey there,

could you elaborate on

*Just connect with User model the fields that are needed*

If I do it like I did in my first app (via *OneToOneField* to *User* from 
*django.contrib.auth.models*), then I will have the fields *first_name* and 
*last_name*, which I won't use.

Could you elaborate?

Best,
Mislav

Dana ponedjeljak, 14. rujna 2020. u 18:47:14 UTC+2 korisnik 
hanz...@gmail.com napisao je:

> You can actually just extend it like the First app.
> 1. Create new Model for Second App authentication.
> 2. Just connect with User model the fields that are needed, the rest you 
> can add it on the new Model
>
> It could be the one of the easy ways.
> Thanks
>
> On Mon, Sep 14, 2020 at 11:00 PM Mislav Jurić  
> wrote:
>
>> Hey guys,
>>
>> as the title says, I've got two apps in my project and each one should 
>> have different authentication model.
>>
>> My first app just extends the User class, which I have done following the 
>> instructions here 
>> <https://docs.djangoproject.com/en/3.1/topics/auth/customizing/#extending-user>.
>>  
>> However, my second app should have a custom User class (with just the 
>> username, email and password) and I have read the instructions here 
>> <https://docs.djangoproject.com/en/3.1/topics/auth/customizing/#auth-custom-user>,
>>  
>> but those instructions imply that I would have to change the configuration 
>> of my project, which I don't want, because my first app uses the default 
>> User class and just extends it.
>>
>> I deleted my database and am starting fresh.
>>
>> *How do I do this?* Do I change the project-wide setting as described 
>> here 
>> <https://docs.djangoproject.com/en/3.1/topics/auth/customizing/#auth-custom-user>,
>>  
>> add the class *User(AbstractUser)* to both of my apps's *models.py* 
>> files and then in my second app create a new user class based on my needs 
>> (i.e. *MyUser(User)*)?
>>
>> Best,
>> Mislav
>>
>> -- 
>> You received this message because you are subscribed to the Google Groups 
>> "Django users" group.
>> To unsubscribe from this group and stop receiving emails from it, send an 
>> email to django-users...@googlegroups.com.
>> To view this discussion on the web visit 
>> https://groups.google.com/d/msgid/django-users/CABTqP_F%3DV1spho7D78QqqfaCVKdUhuPGL1s3wdxdc9okubHKjg%40mail.gmail.com
>>  
>> <https://groups.google.com/d/msgid/django-users/CABTqP_F%3DV1spho7D78QqqfaCVKdUhuPGL1s3wdxdc9okubHKjg%40mail.gmail.com?utm_medium=email_source=footer>
>> .
>>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/9c6a21aa-3716-44e2-b24a-3966173d32e9n%40googlegroups.com.


I've got two apps in my project and each one should have different authentication model. How do achieve this?

2020-09-14 Thread Mislav Jurić
Hey guys,

as the title says, I've got two apps in my project and each one should have
different authentication model.

My first app just extends the User class, which I have done following the
instructions here
.
However, my second app should have a custom User class (with just the
username, email and password) and I have read the instructions here
,
but those instructions imply that I would have to change the configuration
of my project, which I don't want, because my first app uses the default
User class and just extends it.

I deleted my database and am starting fresh.

*How do I do this?* Do I change the project-wide setting as described here
,
add the class *User(AbstractUser)* to both of my apps's *models.py* files
and then in my second app create a new user class based on my needs (i.e.
*MyUser(User)*)?

Best,
Mislav

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CABTqP_F%3DV1spho7D78QqqfaCVKdUhuPGL1s3wdxdc9okubHKjg%40mail.gmail.com.


Re: AttributeError: object has no attribute 'user' while trying to access instance.user.id

2020-09-14 Thread Mislav Jurić
What I can do is first register the employee via a register form, then once 
he logs in ask him/her for the profile picture. *Can I do that this way? *

If I do it this way, I don't have to change my model in any way.

Dana nedjelja, 13. rujna 2020. u 18:11:18 UTC+2 korisnik coolguy napisao je:

> not sure about the purpose of showing that example in Django documentation 
> while its comments are clear that "object will not have been saved to the 
> database yet, so if it uses the default AutoField, *it might not yet have 
> a value for its primary key field*."
>
> so that's the reason for having None with file path.
>
> In this approach what i can see is to save the employee first without file 
> and then edit and select the file.
>
> I would not use this approach and rather keep it simple as followed:
>
> photo = models.ImageField(upload_to='employee/%Y/%m/%d/', blank=True)
>
> On Sunday, September 13, 2020 at 5:41:18 AM UTC-4 mislav@gmail.com 
> wrote:
>
>> Hey coolguy,
>>
>> thanks for responding. After I changed that line as you suggested that 
>> error is solved, *but when I add the user through the admin interface, I 
>> get None as the ID* (the folder that gets created in the */media/users* 
>> is titled *None*). I'm not sure if this is expected behavior.
>>
>> I haven't added the registration or the login yet, so maybe the ID gets a 
>> value when someone is actually registering.
>>
>> Let me know.
>>
>> Best,
>> Mislav
>>
>> Dana subota, 12. rujna 2020. u 21:57:21 UTC+2 korisnik coolguy napisao je:
>>
>>> I wanted to see your model to understand but i realized after my last 
>>> post that you are using "instance.user.id" while your employee instance 
>>> does not have user field. (had to go somewhere urgently) >>> return 
>>> "employees/media/users/{0}/profile_picture.{1}".format(instance.user.id, 
>>> extension)  
>>>
>>> change it as follow and remove user from it.
>>> return "employees/media/users/{0}/profile_picture.{1}".format(
>>> instance.id , extension)  
>>>
>>> It should work...
>>>
>>> On Saturday, September 12, 2020 at 3:17:28 PM UTC-4 mislav@gmail.com 
>>> wrote:
>>>
 coolguy here is the complete Employee model:

 class Employee(models.Model): #TODO: Double-check this
 username = models.CharField(max_length=50, unique=True)
 email = models.EmailField()
 password = models.CharField(max_length=50)
 first_name = models.CharField(max_length=150)
 last_name = models.CharField(max_length=100)
 website = models.URLField(max_length=200, blank=True)

 profile_picture = models.ImageField(upload_to=get_upload_path, 
 blank=True, null=True)
 
 def __str__(self):
 return str(self.first_name) + str(self.last_name)

 *Why do I need the foreign key to User in the first place?* I don't 
 recall seing the foreign key to User in any one of the tutorials.
 Dana subota, 12. rujna 2020. u 20:20:11 UTC+2 korisnik coolguy napisao 
 je:

> Please share the complete employee model. It seems you are missing 
> User foreign key in it.
>
> On Saturday, September 12, 2020 at 2:03:20 PM UTC-4 
> mislav@gmail.com wrote:
>
>> Hey guys,
>>
>> I have the following code in models.py file in one of my apps:
>>
>> def get_upload_path(instance, filename):
>> extension = filename.split('.')[-1]
>> return "employees/media/users/{0}/profile_picture.{1}".format(
>> instance.user.id, extension)
>>
>> class Employee(models.Model):
>> # some attributes here
>> profile_picture = models.ImageField(upload_to=get_upload_path, 
>> blank=True, null=True)
>>
>> I am getting the following error when I try to add an Employee via 
>> the admin interface:
>>
>> AttributeError at /admin/employees/employee/add/
>>
>> 'Employee' object has no attribute 'user'
>>
>> *I don't know where this error is stemming from.* I took the 
>> get_upload_path function from the official Django FIleField 
>> documentation 
>> .
>>
>> Any ideas as to what is going on here?
>>
>> Best,
>> Mislav
>>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/428c96ea-d71b-4174-a8e7-ff5ad45f5ec3n%40googlegroups.com.


How do I make a pre-selectable city and country pairs which will be stored in a model?

2020-09-13 Thread Mislav Jurić
Hey guys,

I want to make a drop-down list of all city an country pairs (such as New
York, USA) where the user, upon registration, will be able to select the
(city, country) pairs from all over Europe and then I want to store that in
one of my models.

*How do I accomplish this (both front-end and back-end)?* I'm focused on
the back-end now, as I'm working on my models now. Which fields do I use
etc.?

Best,
Mislav

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CABTqP_HPBewKRy-C1qiGnGQb7BHHnnEOqPbRvA22OTiFY3QAVQ%40mail.gmail.com.


Re: AttributeError: object has no attribute 'user' while trying to access instance.user.id

2020-09-13 Thread Mislav Jurić
Hey coolguy,

thanks for responding. After I changed that line as you suggested that 
error is solved, *but when I add the user through the admin interface, I 
get None as the ID* (the folder that gets created in the */media/users* is 
titled *None*). I'm not sure if this is expected behavior.

I haven't added the registration or the login yet, so maybe the ID gets a 
value when someone is actually registering.

Let me know.

Best,
Mislav

Dana subota, 12. rujna 2020. u 21:57:21 UTC+2 korisnik coolguy napisao je:

> I wanted to see your model to understand but i realized after my last post 
> that you are using "instance.user.id" while your employee instance does 
> not have user field. (had to go somewhere urgently) >>> return 
> "employees/media/users/{0}/profile_picture.{1}".format(instance.user.id, 
> extension)  
>
> change it as follow and remove user from it.
> return "employees/media/users/{0}/profile_picture.{1}".format(instance.id 
> , extension)  
>
> It should work...
>
> On Saturday, September 12, 2020 at 3:17:28 PM UTC-4 mislav@gmail.com 
> wrote:
>
>> coolguy here is the complete Employee model:
>>
>> class Employee(models.Model): #TODO: Double-check this
>> username = models.CharField(max_length=50, unique=True)
>> email = models.EmailField()
>> password = models.CharField(max_length=50)
>> first_name = models.CharField(max_length=150)
>> last_name = models.CharField(max_length=100)
>> website = models.URLField(max_length=200, blank=True)
>>
>> profile_picture = models.ImageField(upload_to=get_upload_path, 
>> blank=True, null=True)
>> 
>> def __str__(self):
>> return str(self.first_name) + str(self.last_name)
>>
>> *Why do I need the foreign key to User in the first place?* I don't 
>> recall seing the foreign key to User in any one of the tutorials.
>> Dana subota, 12. rujna 2020. u 20:20:11 UTC+2 korisnik coolguy napisao je:
>>
>>> Please share the complete employee model. It seems you are missing User 
>>> foreign key in it.
>>>
>>> On Saturday, September 12, 2020 at 2:03:20 PM UTC-4 mislav@gmail.com 
>>> wrote:
>>>
 Hey guys,

 I have the following code in models.py file in one of my apps:

 def get_upload_path(instance, filename):
 extension = filename.split('.')[-1]
 return "employees/media/users/{0}/profile_picture.{1}".format(
 instance.user.id, extension)

 class Employee(models.Model):
 # some attributes here
 profile_picture = models.ImageField(upload_to=get_upload_path, 
 blank=True, null=True)

 I am getting the following error when I try to add an Employee via the 
 admin interface:

 AttributeError at /admin/employees/employee/add/

 'Employee' object has no attribute 'user'

 *I don't know where this error is stemming from.* I took the 
 get_upload_path function from the official Django FIleField 
 documentation 
 .

 Any ideas as to what is going on here?

 Best,
 Mislav

>>>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/f8662343-2ae8-40f9-a37f-d49bcc8c58c8n%40googlegroups.com.


Re: AttributeError: object has no attribute 'user' while trying to access instance.user.id

2020-09-12 Thread Mislav Jurić
coolguy here is the complete Employee model:

class Employee(models.Model): #TODO: Double-check this
username = models.CharField(max_length=50, unique=True)
email = models.EmailField()
password = models.CharField(max_length=50)
first_name = models.CharField(max_length=150)
last_name = models.CharField(max_length=100)
website = models.URLField(max_length=200, blank=True)
profile_picture = models.ImageField(upload_to=get_upload_path, 
blank=True, null=True)

def __str__(self):
return str(self.first_name) + str(self.last_name)

*Why do I need the foreign key to User in the first place?* I don't recall 
seing the foreign key to User in any one of the tutorials.
Dana subota, 12. rujna 2020. u 20:20:11 UTC+2 korisnik coolguy napisao je:

> Please share the complete employee model. It seems you are missing User 
> foreign key in it.
>
> On Saturday, September 12, 2020 at 2:03:20 PM UTC-4 mislav@gmail.com 
> wrote:
>
>> Hey guys,
>>
>> I have the following code in models.py file in one of my apps:
>>
>> def get_upload_path(instance, filename):
>> extension = filename.split('.')[-1]
>> return "employees/media/users/{0}/profile_picture.{1}".format(
>> instance.user.id, extension)
>>
>> class Employee(models.Model):
>> # some attributes here
>> profile_picture = models.ImageField(upload_to=get_upload_path, 
>> blank=True, null=True)
>>
>> I am getting the following error when I try to add an Employee via the 
>> admin interface:
>>
>> AttributeError at /admin/employees/employee/add/
>>
>> 'Employee' object has no attribute 'user'
>>
>> *I don't know where this error is stemming from.* I took the 
>> get_upload_path function from the official Django FIleField documentation 
>> .
>>
>> Any ideas as to what is going on here?
>>
>> Best,
>> Mislav
>>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/c0e9382d-62d2-4c49-bb83-e3a985d26749n%40googlegroups.com.


Re: I am getting an error message about a non-nullable field in existing rows, even though I dropped all rows via the admin interface

2020-09-12 Thread Mislav Jurić
coolguy:

I tried it without the quotes. Maybe that was the issue.

Dana subota, 12. rujna 2020. u 20:33:52 UTC+2 korisnik coolguy napisao je:

> Keep this handy as well... 
>
> https://docs.djangoproject.com/en/3.1/ref/migration-operations/#addfield
>
> On Saturday, September 12, 2020 at 1:43:37 PM UTC-4 mislav@gmail.com 
> wrote:
>
>> A question to coolguy:
>>
>> *What should I have done if I selected option 1?* I get the Python shell 
>> and if I input a default value - let's say "te...@test.com", what else 
>> do I need to do? I tried to just provide the default value, but it wouldn't 
>> accept it, complaining that it was invalid Python (as it is). *Do I need 
>> to select the existing rows from the database and set them all to have the 
>> value of the new field to the default value? If yes, how do I do this most 
>> efficiently (in code)?*
>>
>> Best,
>> Mislav
>>
>> Dana subota, 12. rujna 2020. u 18:21:14 UTC+2 korisnik coolguy napisao je:
>>
>>> just FYI...
>>>
>>> You didn't have to delete the db.sqllite3 file rather would have 
>>> followed on-screen direction and provided a default value to persist in the 
>>> existing records in your database. Later you could have edited the info 
>>> through your program and make correction.
>>>
>>> This is pretty normal situation where we added new fields/properties in 
>>> our model while database has existing records. You wouldn't be able to 
>>> delete the database in case you are using relational database like postgres 
>>> or mysql.
>>>
>>> Thanks
>>>
>>> On Saturday, September 12, 2020 at 10:44:11 AM UTC-4 
>>> mislav@gmail.com wrote:
>>>
>>>> Hey Danish,
>>>>
>>>> I was able to resolve the error by deleting the *db.sqlite3* file from 
>>>> my project root directory and all of the *migrations* folders from all 
>>>> of my apps.
>>>>
>>>> Thank you for responding.
>>>>
>>>> Best,
>>>> Mislav
>>>>
>>>>
>>>> Dana subota, 12. rujna 2020. u 14:56:13 UTC+2 korisnik 
>>>> mailto...@gmail.com napisao je:
>>>>
>>>>> you need to give default value in email. seems few records are already 
>>>>> exist.
>>>>>
>>>>> else delete sqllite file and try again.
>>>>>
>>>>> On Sat, Sep 12, 2020 at 6:22 PM Mislav Jurić  
>>>>> wrote:
>>>>>
>>>>>> Hey guys,
>>>>>>
>>>>>> I added an EmailField 
>>>>>> <https://docs.djangoproject.com/en/3.1/ref/models/fields/#emailfield> 
>>>>>> to some of my already existing models. When I tried to run:
>>>>>>
>>>>>> *python manage.py makemigrations*
>>>>>>
>>>>>> I got the following prompt:
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>> *You are trying to add a non-nullable field 'email' to employee 
>>>>>> without a default; we can't do that (the database needs something to 
>>>>>> populate existing rows).Please select a fix: 1) Provide a one-off 
>>>>>> default 
>>>>>> now (will be set on all existing rows with a null value for this column) 
>>>>>> 2) 
>>>>>> Quit, and let me add a default in models.pySelect an option: *
>>>>>>
>>>>>> I quit the prompt (option two). Then I went ahead and commented out 
>>>>>> the new EmailField in the models I added them and I dropped all of the 
>>>>>> database rows in my entire database (not just the rows related to the 
>>>>>> models where I added the new email field; I dropped every row from every 
>>>>>> table).
>>>>>>
>>>>>> Then I uncommented the new EmailField and tried to run:
>>>>>>
>>>>>> *python manage.py makemigrations*
>>>>>>
>>>>>> again, *but I still get the prompt above*! I selected option 1 a few 
>>>>>> times, but I'm not sure what I need to do. I tried to supply a value for 
>>>>>> that field, but the prompt is a Python shell, so I'm not sure what I 
>>>>>> need 
>>>>>> to do if I select option 1.
>>>>>>
>>>>>> *How do I fix this?*
>>>>>>
>>>>>> Best,
>>>>>> Mislav
>>>>>>
>>>>>>
>>>>>> -- 
>>>>>> You received this message because you are subscribed to the Google 
>>>>>> Groups "Django users" group.
>>>>>> To unsubscribe from this group and stop receiving emails from it, 
>>>>>> send an email to django-users...@googlegroups.com.
>>>>>> To view this discussion on the web visit 
>>>>>> https://groups.google.com/d/msgid/django-users/CABTqP_HKzXHOAKC-y0AedjsxtBcgKLEk9Cj9J7nhgoD1EpNf%2BA%40mail.gmail.com
>>>>>>  
>>>>>> <https://groups.google.com/d/msgid/django-users/CABTqP_HKzXHOAKC-y0AedjsxtBcgKLEk9Cj9J7nhgoD1EpNf%2BA%40mail.gmail.com?utm_medium=email_source=footer>
>>>>>> .
>>>>>>
>>>>>
>>>>>
>>>>> -- 
>>>>> Thanks & Regards 
>>>>>   
>>>>> Regards, 
>>>>> Danish
>>>>>
>>>>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/ef66474e-3867-43d4-abbe-9183cd08d2cen%40googlegroups.com.


AttributeError: object has no attribute 'user' while trying to access instance.user.id

2020-09-12 Thread Mislav Jurić
Hey guys,

I have the following code in models.py file in one of my apps:

def get_upload_path(instance, filename):
extension = filename.split('.')[-1]
return "employees/media/users/{0}/profile_picture.{1}".format(
instance.user.id, extension)

class Employee(models.Model):
# some attributes here
profile_picture = models.ImageField(upload_to=get_upload_path,
blank=True, null=True)

I am getting the following error when I try to add an Employee via the
admin interface:

AttributeError at /admin/employees/employee/add/

'Employee' object has no attribute 'user'

*I don't know where this error is stemming from.* I took the
get_upload_path function from the official Django FIleField documentation
.

Any ideas as to what is going on here?

Best,
Mislav

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CABTqP_FcyzEXhtyBM--khtnsdHhfszi9vtmt-bY3TUsU0KWmLg%40mail.gmail.com.


Re: I am getting an error message about a non-nullable field in existing rows, even though I dropped all rows via the admin interface

2020-09-12 Thread Mislav Jurić
A question to coolguy:

*What should I have done if I selected option 1?* I get the Python shell 
and if I input a default value - let's say "t...@test.com", what else do I 
need to do? I tried to just provide the default value, but it wouldn't 
accept it, complaining that it was invalid Python (as it is). *Do I need to 
select the existing rows from the database and set them all to have the 
value of the new field to the default value? If yes, how do I do this most 
efficiently (in code)?*

Best,
Mislav

Dana subota, 12. rujna 2020. u 18:21:14 UTC+2 korisnik coolguy napisao je:

> just FYI...
>
> You didn't have to delete the db.sqllite3 file rather would have followed 
> on-screen direction and provided a default value to persist in the existing 
> records in your database. Later you could have edited the info through your 
> program and make correction.
>
> This is pretty normal situation where we added new fields/properties in 
> our model while database has existing records. You wouldn't be able to 
> delete the database in case you are using relational database like postgres 
> or mysql.
>
> Thanks
>
> On Saturday, September 12, 2020 at 10:44:11 AM UTC-4 mislav@gmail.com 
> wrote:
>
>> Hey Danish,
>>
>> I was able to resolve the error by deleting the *db.sqlite3* file from 
>> my project root directory and all of the *migrations* folders from all 
>> of my apps.
>>
>> Thank you for responding.
>>
>> Best,
>> Mislav
>>
>>
>> Dana subota, 12. rujna 2020. u 14:56:13 UTC+2 korisnik 
>> mailto...@gmail.com napisao je:
>>
>>> you need to give default value in email. seems few records are already 
>>> exist.
>>>
>>> else delete sqllite file and try again.
>>>
>>> On Sat, Sep 12, 2020 at 6:22 PM Mislav Jurić  
>>> wrote:
>>>
>>>> Hey guys,
>>>>
>>>> I added an EmailField 
>>>> <https://docs.djangoproject.com/en/3.1/ref/models/fields/#emailfield> 
>>>> to some of my already existing models. When I tried to run:
>>>>
>>>> *python manage.py makemigrations*
>>>>
>>>> I got the following prompt:
>>>>
>>>>
>>>>
>>>>
>>>>
>>>> *You are trying to add a non-nullable field 'email' to employee without 
>>>> a default; we can't do that (the database needs something to populate 
>>>> existing rows).Please select a fix: 1) Provide a one-off default now (will 
>>>> be set on all existing rows with a null value for this column) 2) Quit, 
>>>> and 
>>>> let me add a default in models.pySelect an option: *
>>>>
>>>> I quit the prompt (option two). Then I went ahead and commented out the 
>>>> new EmailField in the models I added them and I dropped all of the 
>>>> database 
>>>> rows in my entire database (not just the rows related to the models where 
>>>> I 
>>>> added the new email field; I dropped every row from every table).
>>>>
>>>> Then I uncommented the new EmailField and tried to run:
>>>>
>>>> *python manage.py makemigrations*
>>>>
>>>> again, *but I still get the prompt above*! I selected option 1 a few 
>>>> times, but I'm not sure what I need to do. I tried to supply a value for 
>>>> that field, but the prompt is a Python shell, so I'm not sure what I need 
>>>> to do if I select option 1.
>>>>
>>>> *How do I fix this?*
>>>>
>>>> Best,
>>>> Mislav
>>>>
>>>>
>>>> -- 
>>>> You received this message because you are subscribed to the Google 
>>>> Groups "Django users" group.
>>>> To unsubscribe from this group and stop receiving emails from it, send 
>>>> an email to django-users...@googlegroups.com.
>>>> To view this discussion on the web visit 
>>>> https://groups.google.com/d/msgid/django-users/CABTqP_HKzXHOAKC-y0AedjsxtBcgKLEk9Cj9J7nhgoD1EpNf%2BA%40mail.gmail.com
>>>>  
>>>> <https://groups.google.com/d/msgid/django-users/CABTqP_HKzXHOAKC-y0AedjsxtBcgKLEk9Cj9J7nhgoD1EpNf%2BA%40mail.gmail.com?utm_medium=email_source=footer>
>>>> .
>>>>
>>>
>>>
>>> -- 
>>> Thanks & Regards 
>>>   
>>> Regards, 
>>> Danish
>>>
>>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/1920b7b0-a595-44e3-9c7f-2897fdb8a58bn%40googlegroups.com.


Re: I am getting an error message about a non-nullable field in existing rows, even though I dropped all rows via the admin interface

2020-09-12 Thread Mislav Jurić
Hey Danish,

I was able to resolve the error by deleting the *db.sqlite3* file from my 
project root directory and all of the *migrations* folders from all of my 
apps.

Thank you for responding.

Best,
Mislav


Dana subota, 12. rujna 2020. u 14:56:13 UTC+2 korisnik mailto...@gmail.com 
napisao je:

> you need to give default value in email. seems few records are already 
> exist.
>
> else delete sqllite file and try again.
>
> On Sat, Sep 12, 2020 at 6:22 PM Mislav Jurić  wrote:
>
>> Hey guys,
>>
>> I added an EmailField 
>> <https://docs.djangoproject.com/en/3.1/ref/models/fields/#emailfield> to 
>> some of my already existing models. When I tried to run:
>>
>> *python manage.py makemigrations*
>>
>> I got the following prompt:
>>
>>
>>
>>
>>
>> *You are trying to add a non-nullable field 'email' to employee without a 
>> default; we can't do that (the database needs something to populate 
>> existing rows).Please select a fix: 1) Provide a one-off default now (will 
>> be set on all existing rows with a null value for this column) 2) Quit, and 
>> let me add a default in models.pySelect an option: *
>>
>> I quit the prompt (option two). Then I went ahead and commented out the 
>> new EmailField in the models I added them and I dropped all of the database 
>> rows in my entire database (not just the rows related to the models where I 
>> added the new email field; I dropped every row from every table).
>>
>> Then I uncommented the new EmailField and tried to run:
>>
>> *python manage.py makemigrations*
>>
>> again, *but I still get the prompt above*! I selected option 1 a few 
>> times, but I'm not sure what I need to do. I tried to supply a value for 
>> that field, but the prompt is a Python shell, so I'm not sure what I need 
>> to do if I select option 1.
>>
>> *How do I fix this?*
>>
>> Best,
>> Mislav
>>
>>
>> -- 
>> You received this message because you are subscribed to the Google Groups 
>> "Django users" group.
>> To unsubscribe from this group and stop receiving emails from it, send an 
>> email to django-users...@googlegroups.com.
>> To view this discussion on the web visit 
>> https://groups.google.com/d/msgid/django-users/CABTqP_HKzXHOAKC-y0AedjsxtBcgKLEk9Cj9J7nhgoD1EpNf%2BA%40mail.gmail.com
>>  
>> <https://groups.google.com/d/msgid/django-users/CABTqP_HKzXHOAKC-y0AedjsxtBcgKLEk9Cj9J7nhgoD1EpNf%2BA%40mail.gmail.com?utm_medium=email_source=footer>
>> .
>>
>
>
> -- 
> Thanks & Regards 
>   
> Regards, 
> Danish
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/e91810c3-1faa-443c-91e1-e8b4c6553b7dn%40googlegroups.com.


Re: I am getting an error message about a non-nullable field in existing rows, even though I dropped all rows via the admin interface

2020-09-12 Thread Mislav Jurić
Hey Danish,

I was able to resolve the error by deleting the *db.sqlite3* file from my 
project root directory and all of the *migration* folders from all of my 
apps.

Thank you for responding.

Best,
Mislav

Dana subota, 12. rujna 2020. u 14:56:13 UTC+2 korisnik mailto...@gmail.com 
napisao je:

> you need to give default value in email. seems few records are already 
> exist.
>
> else delete sqllite file and try again.
>
> On Sat, Sep 12, 2020 at 6:22 PM Mislav Jurić  wrote:
>
>> Hey guys,
>>
>> I added an EmailField 
>> <https://docs.djangoproject.com/en/3.1/ref/models/fields/#emailfield> to 
>> some of my already existing models. When I tried to run:
>>
>> *python manage.py makemigrations*
>>
>> I got the following prompt:
>>
>>
>>
>>
>>
>> *You are trying to add a non-nullable field 'email' to employee without a 
>> default; we can't do that (the database needs something to populate 
>> existing rows).Please select a fix: 1) Provide a one-off default now (will 
>> be set on all existing rows with a null value for this column) 2) Quit, and 
>> let me add a default in models.pySelect an option: *
>>
>> I quit the prompt (option two). Then I went ahead and commented out the 
>> new EmailField in the models I added them and I dropped all of the database 
>> rows in my entire database (not just the rows related to the models where I 
>> added the new email field; I dropped every row from every table).
>>
>> Then I uncommented the new EmailField and tried to run:
>>
>> *python manage.py makemigrations*
>>
>> again, *but I still get the prompt above*! I selected option 1 a few 
>> times, but I'm not sure what I need to do. I tried to supply a value for 
>> that field, but the prompt is a Python shell, so I'm not sure what I need 
>> to do if I select option 1.
>>
>> *How do I fix this?*
>>
>> Best,
>> Mislav
>>
>>
>> -- 
>> You received this message because you are subscribed to the Google Groups 
>> "Django users" group.
>> To unsubscribe from this group and stop receiving emails from it, send an 
>> email to django-users...@googlegroups.com.
>> To view this discussion on the web visit 
>> https://groups.google.com/d/msgid/django-users/CABTqP_HKzXHOAKC-y0AedjsxtBcgKLEk9Cj9J7nhgoD1EpNf%2BA%40mail.gmail.com
>>  
>> <https://groups.google.com/d/msgid/django-users/CABTqP_HKzXHOAKC-y0AedjsxtBcgKLEk9Cj9J7nhgoD1EpNf%2BA%40mail.gmail.com?utm_medium=email_source=footer>
>> .
>>
>
>
> -- 
> Thanks & Regards 
>   
> Regards, 
> Danish
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/ac5c4f4d-8b2f-469d-9e7a-260a415d4644n%40googlegroups.com.


I am getting an error message about a non-nullable field in existing rows, even though I dropped all rows via the admin interface

2020-09-12 Thread Mislav Jurić
Hey guys,

I added an EmailField
 to
some of my already existing models. When I tried to run:

*python manage.py makemigrations*

I got the following prompt:





*You are trying to add a non-nullable field 'email' to employee without a
default; we can't do that (the database needs something to populate
existing rows).Please select a fix: 1) Provide a one-off default now (will
be set on all existing rows with a null value for this column) 2) Quit, and
let me add a default in models.pySelect an option: *

I quit the prompt (option two). Then I went ahead and commented out the new
EmailField in the models I added them and I dropped all of the database
rows in my entire database (not just the rows related to the models where I
added the new email field; I dropped every row from every table).

Then I uncommented the new EmailField and tried to run:

*python manage.py makemigrations*

again, *but I still get the prompt above*! I selected option 1 a few times,
but I'm not sure what I need to do. I tried to supply a value for that
field, but the prompt is a Python shell, so I'm not sure what I need to do
if I select option 1.

*How do I fix this?*

Best,
Mislav

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CABTqP_HKzXHOAKC-y0AedjsxtBcgKLEk9Cj9J7nhgoD1EpNf%2BA%40mail.gmail.com.


Re: I need help with my first Django project - its structure etc.

2020-09-10 Thread Mislav Jurić
Hey Ogunsanya,

before you responded, I started following through the tutorial again, 
modifying the code as needed.* I created one app and in that app I have two 
models* - one for employees, one for companies. I created different URL 
structure and views for employees and companies.

However, as I stated, I only have one app which manages both the employees 
and the companies. You stated that:

*2. Yes you need two apps companies and employees*

*Can you tell me what's the difference between having those two separated 
in different apps vs me just having one app for them both (as I already 
have)?* Will my code be easier to manage if I have them separated in 
different apps? I will, for practice, continue going through the tutorial 
and modifying what I already have, but I'm willing to start from scratch 
with the new design.

It would be immensely helpful for me if you could, in a paragraph or two, 
describe how you would do it (how many apps, how to interconnect them, how 
to do the register/login functionality and should I use tutorials available 
on Google for that etc.).

Best,
Mislav


Dana četvrtak, 10. rujna 2020. u 15:39:45 UTC+2 korisnik Ogunsanya Opeyemi 
napisao je:

> 1. You have to start from there
> 2. Yes you need two apps companies and employees
> 3. Yes your idea is correct
> 4. You can do that.
>
> Email me if you still need help
>
> On Thursday, September 10, 2020, Mislav Jurić  
> wrote:
>
>> Hey guys,
>>
>> I went through the Django tutorial 
>> <https://docs.djangoproject.com/en/3.1/intro/tutorial01/> about 2 weeks 
>> ago and I decided to create a website using Django. I have a couple of 
>> questions on the structure of the Django project, but before I voice them, 
>> I wanted to describe the features that I want my website to have:
>>
>>- there will be two types of users - companies and employees
>>- both companies and employees will have a profile with basic 
>>information about them listed there
>>- there should exist two different registration pages (depending on 
>>if you're a company or if you're an employee)
>>- companies and employees should be able to add certain tags during 
>>their registration (and afterwards add/remove tags via their profile 
>> page) 
>>- companies and employees should be able to search for each other 
>>based on the tags
>>- companies should be able to look at employees profiles and vice 
>>versa
>>- there should exist an administrator website - he/she can remove or 
>>modify the profiles of both companies and employees
>>
>> This may not be all the features I want in the website, but I think you 
>> get the picture.
>>
>> Here are some questions I have for you:
>>
>>1. *Do I start from where the tutorial left off and modify that?*
>>2. *How many apps should my website have?* This is non-trivial for me 
>>to determine as I have things like 2 different types of users, viewing 
>>profiles etc. I'm guessing it's 1 app, but I'm not entirely sure.
>>3. *How do I go about making it possible for the two types of users 
>>to register?* I was thinking about creating separate models for 
>>companies and employees and then making their registration pages 
>> different.
>>4. *Can I somehow hardcode the tags that companies and employees can 
>>use?*
>>
>> These are my questions as of now and I'm sure more will pop up during the 
>> development. I have found some login / registration Django website building 
>> tutorials on Google, but I wanted to check with you to see what the 
>> official Django users say.
>>
>> To recap: *I recently finished the tutorial and I want to make the 
>> website with the features I listed. How do I do that in the least possible 
>> amount of time, keeping my project structure and code clean and 
>> Django-like?*
>>
>> Best,
>> Mislav
>>
>> -- 
>> You received this message because you are subscribed to the Google Groups 
>> "Django users" group.
>> To unsubscribe from this group and stop receiving emails from it, send an 
>> email to django-users+unsubscr...@googlegroups.com.
>> To view this discussion on the web visit 
>> https://groups.google.com/d/msgid/django-users/CABTqP_ENmcouCkdnvQgGw5eR2JEwBh6Dur2YE0z3keGMCUtn-g%40mail.gmail.com
>>  
>> <https://groups.google.com/d/msgid/django-users/CABTqP_ENmcouCkdnvQgGw5eR2JEwBh6Dur2YE0z3keGMCUtn-g%40mail.gmail.com?utm_medium=email_source=footer>
>> .
>>
>
>
> -- 
> OGUNSANYA OPEYEMI
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/190c4ca9-7fc3-4979-9176-aa1c3d127d42n%40googlegroups.com.


Re: I need help with my first Django project - its structure etc.

2020-09-10 Thread Mislav Jurić
Hey Ogunsanya,

before you responded, I started following through the tutorial again, 
modifying the code as needed.* I created one app and in that app I have two 
models* - one for employees, one for companies. I created different URL 
structure and views for employees and companies.

However, as I stated, I only have one app which manages both the employees 
and the companies. You stated that:

*2. Yes you need two apps companies and employees*

*Can you tell me what's the difference between having those two separated 
in different apps vs me just having one app for them both (as I already 
have)?* Will my code be easier to manage if I have them separated in 
different apps? I will, for practice, continue going through the tutorial 
and modifying what I already have, but I'm willing to start from scratch 
with the new design.

It would be immensely helpful for me if you could, in a paragraph or two, 
describe how you would do it (how many apps, how to interconnect them, 
etc.).

Best,
Mislav
Dana četvrtak, 10. rujna 2020. u 15:39:45 UTC+2 korisnik Ogunsanya Opeyemi 
napisao je:

> 1. You have to start from there
> 2. Yes you need two apps companies and employees
> 3. Yes your idea is correct
> 4. You can do that.
>
> Email me if you still need help
>
> On Thursday, September 10, 2020, Mislav Jurić  
> wrote:
>
>> Hey guys,
>>
>> I went through the Django tutorial 
>> <https://docs.djangoproject.com/en/3.1/intro/tutorial01/> about 2 weeks 
>> ago and I decided to create a website using Django. I have a couple of 
>> questions on the structure of the Django project, but before I voice them, 
>> I wanted to describe the features that I want my website to have:
>>
>>- there will be two types of users - companies and employees
>>- both companies and employees will have a profile with basic 
>>information about them listed there
>>- there should exist two different registration pages (depending on 
>>if you're a company or if you're an employee)
>>- companies and employees should be able to add certain tags during 
>>their registration (and afterwards add/remove tags via their profile 
>> page) 
>>- companies and employees should be able to search for each other 
>>based on the tags
>>- companies should be able to look at employees profiles and vice 
>>versa
>>- there should exist an administrator website - he/she can remove or 
>>modify the profiles of both companies and employees
>>
>> This may not be all the features I want in the website, but I think you 
>> get the picture.
>>
>> Here are some questions I have for you:
>>
>>1. *Do I start from where the tutorial left off and modify that?*
>>2. *How many apps should my website have?* This is non-trivial for me 
>>to determine as I have things like 2 different types of users, viewing 
>>profiles etc. I'm guessing it's 1 app, but I'm not entirely sure.
>>3. *How do I go about making it possible for the two types of users 
>>to register?* I was thinking about creating separate models for 
>>companies and employees and then making their registration pages 
>> different.
>>4. *Can I somehow hardcode the tags that companies and employees can 
>>use?*
>>
>> These are my questions as of now and I'm sure more will pop up during the 
>> development. I have found some login / registration Django website building 
>> tutorials on Google, but I wanted to check with you to see what the 
>> official Django users say.
>>
>> To recap: *I recently finished the tutorial and I want to make the 
>> website with the features I listed. How do I do that in the least possible 
>> amount of time, keeping my project structure and code clean and 
>> Django-like?*
>>
>> Best,
>> Mislav
>>
>> -- 
>> You received this message because you are subscribed to the Google Groups 
>> "Django users" group.
>> To unsubscribe from this group and stop receiving emails from it, send an 
>> email to django-users+unsubscr...@googlegroups.com.
>> To view this discussion on the web visit 
>> https://groups.google.com/d/msgid/django-users/CABTqP_ENmcouCkdnvQgGw5eR2JEwBh6Dur2YE0z3keGMCUtn-g%40mail.gmail.com
>>  
>> <https://groups.google.com/d/msgid/django-users/CABTqP_ENmcouCkdnvQgGw5eR2JEwBh6Dur2YE0z3keGMCUtn-g%40mail.gmail.com?utm_medium=email_source=footer>
>> .
>>
>
>
> -- 
> OGUNSANYA OPEYEMI
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/3d7529a6-6e37-47d4-aa16-b3180539135cn%40googlegroups.com.


I need help with my first Django project - its structure etc.

2020-09-10 Thread Mislav Jurić
Hey guys,

I went through the Django tutorial
 about 2 weeks ago
and I decided to create a website using Django. I have a couple of
questions on the structure of the Django project, but before I voice them,
I wanted to describe the features that I want my website to have:

   - there will be two types of users - companies and employees
   - both companies and employees will have a profile with basic
   information about them listed there
   - there should exist two different registration pages (depending on if
   you're a company or if you're an employee)
   - companies and employees should be able to add certain tags during
   their registration (and afterwards add/remove tags via their profile page)
   - companies and employees should be able to search for each other based
   on the tags
   - companies should be able to look at employees profiles and vice versa
   - there should exist an administrator website - he/she can remove or
   modify the profiles of both companies and employees

This may not be all the features I want in the website, but I think you get
the picture.

Here are some questions I have for you:

   1. *Do I start from where the tutorial left off and modify that?*
   2. *How many apps should my website have?* This is non-trivial for me to
   determine as I have things like 2 different types of users, viewing
   profiles etc. I'm guessing it's 1 app, but I'm not entirely sure.
   3. *How do I go about making it possible for the two types of users to
   register?* I was thinking about creating separate models for companies
   and employees and then making their registration pages different.
   4. *Can I somehow hardcode the tags that companies and employees can
   use?*

These are my questions as of now and I'm sure more will pop up during the
development. I have found some login / registration Django website building
tutorials on Google, but I wanted to check with you to see what the
official Django users say.

To recap: *I recently finished the tutorial and I want to make the website
with the features I listed. How do I do that in the least possible amount
of time, keeping my project structure and code clean and Django-like?*

Best,
Mislav

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CABTqP_ENmcouCkdnvQgGw5eR2JEwBh6Dur2YE0z3keGMCUtn-g%40mail.gmail.com.