Re: IntegrityError: null value in column "user_id" violates not-null constraint

2020-07-30 Thread Dinolin yp job
Thanks a lot @obastephen it worked.

On Friday, July 31, 2020 at 12:32:47 AM UTC+5:30 obast...@gmail.com wrote:

> irst in your UploadSerializer fields use fields = "__all__"
>
> Then you can use this in your views:
>
> *def post(self, **request, format=None**):*
> *upload_file = request.data.get("upload_file"**)*
> *upload_date = **request.data.get("upload_date"**)*
>
>
> *user = request.userdata = {'upload_file': upload_file, 
> 'upload_date': upload_date, 'user': user}*
> *serializer = **UploadSerializer**(data=data)*
>
>
>
>
> *if serializer.is_valid():serializer.save()
> return Response(serializer.data, status=status.HTTP_201_CREATED)    
> return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)*
> On Thu, Jul 30, 2020 at 5:59 PM Dinolin yp job  
> wrote:
>
>> I don't have forms.py since I'm creating only the endpoints using rest 
>> framework. I've done it earlier and it has worked but earlier the foreign 
>> key was to refer the default User model but now I have customized User 
>> model by extending AbstractBaseUser. 
>>
>> uploads/views.py
>>
>>
>>
>>
>>
>>
>> *class UploadView(APIView):def post(self, request, 
>> format=None):serializer = 
>> UploadSerializer(data=request.data)if 
>> serializer.is_valid():serializer.save()return 
>> Response(serializer.data, status=status.HTTP_201_CREATED)return 
>> Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)*
>>
>> uploads/serializers.py
>>
>>
>>
>> *class UploadSerializer(serializers.ModelSerializer):class Meta:
>> model = Uploadfields = ('upload_file', 'upload_date')*
>>
>> users/models.py
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>> *class User(AbstractBaseUser):user_id = 
>> models.AutoField(primary_key=True)username = 
>> models.CharField(max_length=50, unique=True)first_name = 
>> models.CharField(max_length=50)last_name = 
>> models.CharField(max_length=50)company_name = 
>> models.CharField(max_length=100)email = 
>> models.EmailField(max_length=150, unique=True)is_active = 
>> models.BooleanField(default=True)is_superuser = 
>> models.BooleanField(default=False)objects = UserManager()
>> USERNAME_FIELD = 'username'REQUIRED_FIELDS = ['first_name', 
>> 'last_name', 'company_name', 'email']def get_full_name(self):
>> return self.first_name + self.last_name*
>>
>>
>> On Thursday, July 30, 2020 at 7:59:38 PM UTC+5:30 obast...@gmail.com 
>> wrote:
>>
>>> You can create an upload form with an Image field, then when saving the 
>>> for set the User field to request.user.
>>>
>>> Do you have an UploadForm already in your forms.py? if yes, show me with 
>>> the view that handles.
>>>
>>> On Thu, Jul 30, 2020 at 11:59 AM Dinolin yp job  
>>> wrote:
>>>
>>>>
>>>>
>>>> It worked but user_id column in uploads_upload table is empty. How to 
>>>> solve this issue?
>>>> On Thursday, July 30, 2020 at 3:31:21 PM UTC+5:30 obast...@gmail.com 
>>>> wrote:
>>>>
>>>>> From the error, it states that you are trying to add an upload object 
>>>>> to the database without a user. And from your model.py, the User foreign 
>>>>> key does not have a null=True, and blank=True. So you can't save a null 
>>>>> value for that user field. 
>>>>>
>>>>> On Thu, Jul 30, 2020 at 10:52 AM Dinolin yp job  
>>>>> wrote:
>>>>>
>>>>>> I'm trying to save image in postgres database. I have upload model 
>>>>>> which has a foreign key reference to the extended custom user model. But 
>>>>>> it 
>>>>>> shows the following error
>>>>>>
>>>>>>
>>>>>> uploads/model.py
>>>>>> from django.db import models
>>>>>> from django.contrib.auth import get_user_model
>>>>>>
>>>>>> User = get_user_model()
>>>>>>
>>>>>> class Upload(models.Model):
>>>>>> upload_file = mo

Re: IntegrityError: null value in column "user_id" violates not-null constraint

2020-07-30 Thread Dinolin yp job
I don't have forms.py since I'm creating only the endpoints using rest 
framework. I've done it earlier and it has worked but earlier the foreign 
key was to refer the default User model but now I have customized User 
model by extending AbstractBaseUser. 

uploads/views.py






*class UploadView(APIView):def post(self, request, format=None):
serializer = UploadSerializer(data=request.data)if 
serializer.is_valid():serializer.save()return 
Response(serializer.data, status=status.HTTP_201_CREATED)return 
Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)*

uploads/serializers.py



*class UploadSerializer(serializers.ModelSerializer):class Meta:
model = Uploadfields = ('upload_file', 'upload_date')*

users/models.py
















*class User(AbstractBaseUser):user_id = 
models.AutoField(primary_key=True)username = 
models.CharField(max_length=50, unique=True)first_name = 
models.CharField(max_length=50)last_name = 
models.CharField(max_length=50)company_name = 
models.CharField(max_length=100)email = 
models.EmailField(max_length=150, unique=True)is_active = 
models.BooleanField(default=True)is_superuser = 
models.BooleanField(default=False)objects = UserManager()
USERNAME_FIELD = 'username'REQUIRED_FIELDS = ['first_name', 
'last_name', 'company_name', 'email']def get_full_name(self):
return self.first_name + self.last_name*


On Thursday, July 30, 2020 at 7:59:38 PM UTC+5:30 obast...@gmail.com wrote:

> You can create an upload form with an Image field, then when saving the 
> for set the User field to request.user.
>
> Do you have an UploadForm already in your forms.py? if yes, show me with 
> the view that handles.
>
> On Thu, Jul 30, 2020 at 11:59 AM Dinolin yp job  
> wrote:
>
>>
>>
>> It worked but user_id column in uploads_upload table is empty. How to 
>> solve this issue?
>> On Thursday, July 30, 2020 at 3:31:21 PM UTC+5:30 obast...@gmail.com 
>> wrote:
>>
>>> From the error, it states that you are trying to add an upload object to 
>>> the database without a user. And from your model.py, the User foreign key 
>>> does not have a null=True, and blank=True. So you can't save a null value 
>>> for that user field. 
>>>
>>> On Thu, Jul 30, 2020 at 10:52 AM Dinolin yp job  
>>> wrote:
>>>
>>>> I'm trying to save image in postgres database. I have upload model 
>>>> which has a foreign key reference to the extended custom user model. But 
>>>> it 
>>>> shows the following error
>>>>
>>>>
>>>> uploads/model.py
>>>> from django.db import models
>>>> from django.contrib.auth import get_user_model
>>>>
>>>> User = get_user_model()
>>>>
>>>> class Upload(models.Model):
>>>> upload_file = models.ImageField(upload_to='uploads/')
>>>> upload_date = models.DateTimeField(auto_now_add =True)
>>>> user = models.ForeignKey(User, on_delete=models.CASCADE)
>>>>
>>>>
>>>> What am I missing? Thanks in advance
>>>>
>>>> -- 
>>>> 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/bddd1f92-895c-4e23-8f08-976c0f178f17o%40googlegroups.com
>>>>  
>>>> <https://groups.google.com/d/msgid/django-users/bddd1f92-895c-4e23-8f08-976c0f178f17o%40googlegroups.com?utm_medium=email&utm_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...@googlegroups.com.
>>
> To view this discussion on the web visit 
>> https://groups.google.com/d/msgid/django-users/e3462364-f867-4187-80bd-abfaa09d86b8n%40googlegroups.com
>>  
>> <https://groups.google.com/d/msgid/django-users/e3462364-f867-4187-80bd-abfaa09d86b8n%40googlegroups.com?utm_medium=email&utm_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/0ce310b5-7bf5-4200-8ddd-e679c160669dn%40googlegroups.com.


Re: IntegrityError: null value in column "user_id" violates not-null constraint

2020-07-30 Thread Dinolin yp job


It worked but user_id column in uploads_upload table is empty. How to solve 
this issue?
On Thursday, July 30, 2020 at 3:31:21 PM UTC+5:30 obast...@gmail.com wrote:

> From the error, it states that you are trying to add an upload object to 
> the database without a user. And from your model.py, the User foreign key 
> does not have a null=True, and blank=True. So you can't save a null value 
> for that user field. 
>
> On Thu, Jul 30, 2020 at 10:52 AM Dinolin yp job  
> wrote:
>
>> I'm trying to save image in postgres database. I have upload model which 
>> has a foreign key reference to the extended custom user model. But it shows 
>> the following error
>>
>>
>> uploads/model.py
>> from django.db import models
>> from django.contrib.auth import get_user_model
>>
>> User = get_user_model()
>>
>> class Upload(models.Model):
>> upload_file = models.ImageField(upload_to='uploads/')
>> upload_date = models.DateTimeField(auto_now_add =True)
>> user = models.ForeignKey(User, on_delete=models.CASCADE)
>>
>>
>> What am I missing? Thanks in advance
>>
>> -- 
>> 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/bddd1f92-895c-4e23-8f08-976c0f178f17o%40googlegroups.com
>>  
>> <https://groups.google.com/d/msgid/django-users/bddd1f92-895c-4e23-8f08-976c0f178f17o%40googlegroups.com?utm_medium=email&utm_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/e3462364-f867-4187-80bd-abfaa09d86b8n%40googlegroups.com.


Re: IntegrityError: null value in column "user_id" violates not-null constraint

2020-07-30 Thread Dinolin yp job


It worked but the user_id in uploads_upload is empty. How can I can make 
that work?
On Thursday, July 30, 2020 at 3:31:21 PM UTC+5:30 obast...@gmail.com wrote:

> From the error, it states that you are trying to add an upload object to 
> the database without a user. And from your model.py, the User foreign key 
> does not have a null=True, and blank=True. So you can't save a null value 
> for that user field. 
>
> On Thu, Jul 30, 2020 at 10:52 AM Dinolin yp job  
> wrote:
>
>> I'm trying to save image in postgres database. I have upload model which 
>> has a foreign key reference to the extended custom user model. But it shows 
>> the following error
>>
>>
>> uploads/model.py
>> from django.db import models
>> from django.contrib.auth import get_user_model
>>
>> User = get_user_model()
>>
>> class Upload(models.Model):
>> upload_file = models.ImageField(upload_to='uploads/')
>> upload_date = models.DateTimeField(auto_now_add =True)
>> user = models.ForeignKey(User, on_delete=models.CASCADE)
>>
>>
>> What am I missing? Thanks in advance
>>
>> -- 
>> 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/bddd1f92-895c-4e23-8f08-976c0f178f17o%40googlegroups.com
>>  
>> <https://groups.google.com/d/msgid/django-users/bddd1f92-895c-4e23-8f08-976c0f178f17o%40googlegroups.com?utm_medium=email&utm_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/57100db3-81b7-49ca-86e6-096c121c94c8n%40googlegroups.com.


IntegrityError: null value in column "user_id" violates not-null constraint

2020-07-30 Thread Dinolin yp job
I'm trying to save image in postgres database. I have upload model which 
has a foreign key reference to the extended custom user model. But it shows 
the following error


uploads/model.py
from django.db import models
from django.contrib.auth import get_user_model

User = get_user_model()

class Upload(models.Model):
upload_file = models.ImageField(upload_to='uploads/')
upload_date = models.DateTimeField(auto_now_add =True)
user = models.ForeignKey(User, on_delete=models.CASCADE)


What am I missing? Thanks in advance

-- 
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/bddd1f92-895c-4e23-8f08-976c0f178f17o%40googlegroups.com.