I am trying to create a model using django 1.11
and mysql
(latest) as my backend using mysqlclient
. I have searched various blogs and docs but was still not able to find my solution.
This is my code Posts.models.py
Please forgive the indentation error here if any.
class Post(models.Model):
user=models.ForeignKey(User,related_name='posts',
on_delete=models.CASCADE)
created_at=models.DateTimeField(auto_now=True)
message=models.TextField()
group=models.ForeignKey(Group,related_name='posts',
null=True,blank=True,on_delete=models.CASCADE)
def __str__(self):
return self.message
def save(self,*args,**kwargs):
super().save(*args,**kwargs)
def get_absolute_url(self):
return reverse('posts:single',kwargs{'username':self.user.username,'pk':self.pk})
class Meta:
ordering=['-created_at']
unique_together=['user','message']