Issue
currently i am in a very weird situation. I am trying to upload document using article id and user id to an article. But the issue is when I try to select article id from the document model, it gives error that article doesnt exists.
And tbh that is true, because how can i upload document to an article when it doesnt even exists. So how can i use article id in such situation?
Below is my document model in which i am sending user id and article id for uploading document.
documentmodels.py
class DocumentModel(models.Model):
id=models.AutoField(primary_key=True, auto_created=True, verbose_name="DOCUMENT_ID")
user_fk_doc=models.ForeignKey(User, on_delete=models.CASCADE, related_name="users_fk_doc")
article_fk_doc=models.ForeignKey(Article, on_delete=models.CASCADE, related_name="articles_fk_doc")
document=models.FileField(max_length=350, validators=[FileExtensionValidator(extensions)], upload_to=uploaded_files)
filename=models.CharField(max_length=100, blank=True)
filesize=models.IntegerField(default=0, blank=True)
mimetype=models.CharField(max_length=100, blank=True)
created_at=models.DateField(auto_now_add=True)
and below is the articles models,
class Article(models.Model):
id=models.AutoField(primary_key=True, auto_created=True, verbose_name="ARTICLE_ID")
headline=models.CharField(max_length=250)
abstract=models.TextField(max_length=1500, blank=True)
content=models.TextField(max_length=10000, blank=True)
published=models.DateTimeField(auto_now_add=True)
tags=models.ManyToManyField('Tags', related_name='tags', blank=True)
isDraft=models.BooleanField(blank=True, default=False)
isFavourite=models.ManyToManyField(User, related_name="favourite", blank=True)
created_by=models.ForeignKey(User, on_delete=models.CASCADE, null=True, blank=True, related_name="articles")
EDITED
Actually i am trying to implement upload file feature in article which is in editor. I can upload the document for the article according to user but the issue is in the start:
1: There is no article in the beginning so I have no article id
2: So if I have no article id, I cannot upload the document without article id else it throws error that field cannot be null, or field s required
3: I want to know how can I solve the issue where in the beginning I dont have any article id and upload the document
document views.py
class DocumentViewSet(viewsets.ModelViewSet):
serializer_class=DocumentSerializer
permission_classes=[permissions.IsAuthenticated]
authentication_classes= [authentication.TokenAuthentication]
parser_classes=[FormParser, MultiPartParser]
def get_queryset(self):
return DocumentModel.objects.select_related('user_fk_doc').all()
#Cache
@action(detail=True, methods =['get'], url_path='download')
def download(self, request, pk):
try:
document_file=DocumentModel.objects.get(id=pk, user_fk_doc=self.request.user)
file_path=document_file.document.path
print(file_path)
if os.path.exists(file_path):
with open(file_path, 'rb') as fh:
response=HttpResponse(fh.read(), content_type=mimetypes.guess_type(file_path)[0])
response['Content-Disposition'] = "Inline; filename={}".format(os.path.basename(file_path))
response['Content-Length'] = os.path.getsize(file_path)
return response
return Response({'error' : 'There is no document file of the user'}, status=status.HTTP_403_FORBIDDEN)
except DocumentModel.DoesNotExist as e:
return Response({'error': 'Document for this user does not exists'}, status=status.HTTP_404_NOT_FOUND)
Article views.py
class ArticleViewSet(viewsets.ModelViewSet):
queryset=Article.objects.all()
serializer_class=ArticleSerializer
permission_classes=[permissions.IsAuthenticated]
authentication_classes = [authentication.TokenAuthentication]
Solution
So, I think the way I was trying to post document for article was complicated. I was adding the article id into document when article id was not even created till then.
So the solution that I came upon is instead of using these two foreign keys in document model below:
user_fk_doc=models.ForeignKey(User, on_delete=models.CASCADE, related_name="users_fk_doc")
article_fk_doc=models.ForeignKey(Article, on_delete=models.CASCADE, related_name="articles_fk_doc")
I simply added a many to many field in article model. so my model became like this below:
class Article(models.Model):
id=models.AutoField(primary_key=True, auto_created=True, verbose_name="ARTICLE_ID")
headline=models.CharField(max_length=250)
abstract=models.TextField(max_length=1500, blank=True)
content=models.TextField(max_length=10000, blank=True)
published=models.DateTimeField(auto_now_add=True)
tags=models.ManyToManyField('Tags', related_name='tags', blank=True)
**files=models.ManyToManyField('DocumentModel', related_name='uploads', blank=True)**
isDraft=models.BooleanField(blank=True, default=False)
isFavourite=models.ManyToManyField(User, related_name="favourite", blank=True)
created_by=models.ForeignKey(User, on_delete=models.CASCADE, null=True, blank=True, related_name="articles")
and then simply upload document through document model and then use many to many field to filter all the documents according to user and article.
In the end simply execute such query depending on your situation to filter documents
DocumentModel.objects.get(id=pk,uploads__created_by_id=self.request.user.id)
Answered By - Abdullah Roshan
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.