I learning python and django and trying to make some sort of simple forum. I created models:
class Board(models.Model):
title = models.CharField(max_length=200)
description = models.TextField(blank = True)
class Post(models.Model):
board = models.ForeignKey(Board)
...
I’d like to have post numeration starting from 0 for every board. How i should change my models to achieve that? What am i doing wrong?
I don’t know if you’re talking about using the primary key as the post ID, but that won’t work if you want it to start with 0 for each board. I’d recommend adding another field on your
Postmodel and manage it yourself on thesavemethod. Example:Edit: As Brian points out, there could be problems when posts are deleted. This would result in duplicate
post_ids. You could add adeletedfield to the model to “fake” delete posts (don’t show whendeleted= True) which would keep thepost_idcounting accurate.Edit 2: You could set the
post_countto be equal to the maximum post count for this board + 1 (or 0 if the count is 0)