How to upload document on an Article using article id when it does not even exists
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")
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)