GitHub issue ids start at 1 for each repository, how can I replicate this in Django?

筅森魡賤 提交于 2020-01-06 02:50:08

问题


On Github, the first issue for each repository has id 1, despite there being many other issues on GitHub.

https://github.com/johndoe/foo/issues/1

How can I accomplish this in Django, so that a model has an id that only increments in relation to a model it is related to?


回答1:


You can simply create an extra unique field on the model, here's an example

class MyModel(models.Model):
    user = models.ForeignKey(User)
    internal_id = models.CharField(verbose_name=_(u"Internal ID"), max_length=7)

    @classmethod
    def generate_internal_id(cls, self):
        try:
            # note: you could also implement 'lastest' which is more readable
            return int(cls.objects.filter(user=self.user).order_by('-id')[0].id) + 1
        except (IndexError, cls.DoesNotExist):
            return SOME_INTERNAL_ID_OFFSET

    def clean(self):
        if not self.id:
            self.internal_id = self.generate_internal_id()
        return super(MyModel, self).clean()



回答2:


You need to create another field for such a thing. In database, every row must have unique primary key across all rows in that table. In SQL it is possible to declare compound primary key, but it is not currently supported in django.

Sample code:

from django.db.models.signals import pre_save


class Repository(models.Model):
    # ...


class Issue(model.Model):
    number = models.IntegerField(default=1)
    repo = models.ForeignKey(Repository)
    # ...

    class Meta:
        unique_together = (('repo', 'number'),)

    @classmethod
    def assign_number(cls, sender, instance, **kwars):
        if not instance.pk:
            # Only for new instances
            try:
                instance.number = cls.objects.filter(repo=instance.repo)\
                    .order_by('-number')[0].number + 1
            except IndexError:
                pass


pre_save.connect(Issue.assign_number, sender=Issue)

UPDATED

Code will assign next number to newly created issues. It is strong to issues deletion. But, if last issue (with highest number) was deleted, then new issue will assign again the same number, as deleted issue.



来源:https://stackoverflow.com/questions/26229997/github-issue-ids-start-at-1-for-each-repository-how-can-i-replicate-this-in-dja

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!