models.py
class Post(models.Model):
poster = models.ForeignKey(User, on_delete=models.CASCADE)
content = models.TextField(max_length=500)
timestamp = models.DateTimeField(default=timezone.now)
likes = models.ManyToManyField(User, blank=True, related_name='likes')
views.py
def index(request):
if request.method == 'POST':
data = json.loads(request.body)
# postid = data.get('postid', '')
postcontent = data.get('postcontent', '')
print(postcontent)
obj, created = Post.objects.update_or_create(
poster=request.user, content=postcontent,
defaults={'content': postcontent})
by using update_or_create
views.py
is able to create new entries (). But, on the time ofedit
, it createsnew
entry (). I know that , method update_or_create need some unique entity (anid
for instance) which _view _or JS’s fetch can’t generate while creating newobject
… whatunique
value can go inside _update_orcreate method ?