Adding a verbose_name to id field

129 views
Skip to first unread message

Bruckner de Villiers

unread,
Feb 8, 2020, 5:34:02 AM2/8/20
to django...@googlegroups.com

Using Sqlite3 & Django 3.0.2 - I have searched high and low, but can’t find a way to add a verbose name to the id of a model.

Anyone have ideas, please?

 

Bruckner de Villiers

Mike Dewhirst

unread,
Feb 8, 2020, 6:48:56 AM2/8/20
to django...@googlegroups.com
I imagine you need to specify the id field in your model and give it a verbose name. 

This is an unusual requirement. Why do you want to do such a thing?

Mike

Bruckner de Villiers

unread,
Feb 8, 2020, 11:55:48 AM2/8/20
to django...@googlegroups.com

Mike,

I tried id = model.AutoField(verbose_name=”Ticket #”).  Migrate didn’t like it – can’t have duplicate primaries.  I suspect this is because the Bug model already exists.

 

What I am trying to achieve is an auto-increment field whereby every time a user logs a bug/suggestion/comment it writes a unique ticket number in the background, which is used as a reference for subsequent actions like Fixed, Close, WiP, etc.  and as a ForeignKey in the Comment model.   I couldn’t find anything meaningful for this functionality and so thought that the ‘id’ is already fit for purpose, but merely requires an explanatory field text.

 

Make sense?

 

Bruckner de Villiers

083 625 1086

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/5e3e9fe6.1c69fb81.7cda5.a2fdSMTPIN_ADDED_MISSING%40gmr-mx.google.com.

Jason

unread,
Feb 8, 2020, 7:47:00 PM2/8/20
to Django users
Sounds to me you'd like a separate auto-increment field?

To unsubscribe from this group and stop receiving emails from it, send an email to django...@googlegroups.com.

Mike Dewhirst

unread,
Feb 9, 2020, 1:28:35 AM2/9/20
to django...@googlegroups.com
On 9/02/2020 3:54 am, Bruckner de Villiers wrote:
>
> Mike,
>
> I tried id = model.AutoField(verbose_name=”Ticket #”).  Migrate didn’t
> like it – can’t have duplicate primaries.  I suspect this is because
> the Bug model already exists.
>
> What I am trying to achieve is an auto-increment field whereby every
> time a user logs a bug/suggestion/comment it writes a unique ticket
> number in the background, which is used as a reference for subsequent
> actions like Fixed, Close, WiP, etc.  and as a ForeignKey in the
> Comment model.   I couldn’t find anything meaningful for this
> functionality and so thought that the ‘id’ is already fit for purpose,
> but merely requires an explanatory field text.
>

I think Jason is correct. The id field belongs to the DBMS and
theoretically should never carry any human or real world meaning.

You would be better having an auto-incrementing field called "ticket" or
similar

If you really must use the id field you can supply the model with your
own id field (that's covered somewhere in the docs) with whatever name
suits.

Cheers

Mike



> Make sense?
>
> Bruckner de Villiers
>
> 083 625 1086
>
> *From: *<django...@googlegroups.com> on behalf of Mike Dewhirst
> <mi...@dewhirst.com.au>
> *Reply to: *<django...@googlegroups.com>
> *Date: *Saturday, 08 February 2020 at 13:48
> *To: *<django...@googlegroups.com>
> *Subject: *RE: Adding a verbose_name to id field
>
> I imagine you need to specify the id field in your model and give it a
> verbose name.
>
> This is an unusual requirement. Why do you want to do such a thing?
>
> Mike
>
> -------- Original message --------
>
> From: Bruckner de Villiers <bruckner....@gmail.com>
>
> Date: 8/2/20 21:33 (GMT+10:00)
>
> To: django...@googlegroups.com
>
> Subject: Adding a verbose_name to id field
>
> Using Sqlite3 & Django 3.0.2 - I have searched high and low, but can’t
> find a way to add a verbose name to the id of a model.
>
> Anyone have ideas, please?
>
> Bruckner de Villiers
>
> +27 83 625 1086
>
> --
> You received this message because you are subscribed to the Google
> Groups "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to django-users...@googlegroups.com
> <mailto:django-users...@googlegroups.com>.
> <https://groups.google.com/d/msgid/django-users/5e3e9fe6.1c69fb81.7cda5.a2fdSMTPIN_ADDED_MISSING%40gmr-mx.google.com?utm_medium=email&utm_source=footer>.
>
> --
> You received this message because you are subscribed to the Google
> Groups "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to django-users...@googlegroups.com
> <mailto:django-users...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/CA217C44-C4FE-4CE8-8479-97B9F05939BE%40gmail.com
> <https://groups.google.com/d/msgid/django-users/CA217C44-C4FE-4CE8-8479-97B9F05939BE%40gmail.com?utm_medium=email&utm_source=footer>.

Bruckner de Villiers

unread,
Feb 9, 2020, 12:21:30 PM2/9/20
to django...@googlegroups.com
Thank you Mike & Jason.

I agree - messing around with the id is not the brightest idea I've ever conceived.
However, the Django 3 documentation is somewhat mute on auto-incrementing, but I'll continue searching Stack & Mozilla. Have a great evening (I am GMT+2).
Regards,

Bruckner de Villiers
083 625 1086

To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/2ad77dc8-e743-8241-7531-378bb7f717af%40dewhirst.com.au.



Jason

unread,
Feb 9, 2020, 6:20:04 PM2/9/20
to Django users
You just need to use AutoField

Bruckner de Villiers

unread,
Feb 10, 2020, 3:58:36 AM2/10/20
to django...@googlegroups.com

 

Thank you Jason.  However, I get the following error - AssertionError: Model bugs.Bugs can't have more than one auto-generated field.  Before adding the underlined bug_ticket field everything worked.  Is the error because the model already exists and I am adding the AutoField?

 

My models.py:

 

class Bugs(models.Model):

    FUNCTION_CHOICES = [

        ('C', 'Contacts'),

        ('E', 'Execs'),

        ('I', 'Sector/Industry'),

        ('G', 'General'),

        ('L', 'Clients'),

        ('O', 'Organisations'),

        ('P', 'Opportunities'),

        ('S', 'Account Type'),

        ('T', 'Training Material'),

        ('U', 'Currencies'),

       

    ]

    BUGTYPE_CHOICES = [

        ('B', 'Bug'),

        ('C', 'Comment'),

        ('S', 'Suggestion'),

    ]

    CLOSED_CHOICES = [

        ('N', 'No'),

        ('W', 'Work in Progress'),

        ('Y', 'Yes'),

    ]

    bug_ticket = models.AutoField(primary_key=False)

    bug_author = models.ForeignKey('auth.User', on_delete=models.CASCADE, verbose_name="Issue Author")

    bug_title = models.CharField(max_length=200, verbose_name="Brief Description:")

    bug_type = models.CharField(max_length=1, choices=BUGTYPE_CHOICES, verbose_name="Issue Type")

    bug_function = models.CharField(max_length=1, default="G", choices=FUNCTION_CHOICES, verbose_name="Function")

    bug_text = models.TextField()

    bug_status = models.CharField(max_length=1, default="N", choices=CLOSED_CHOICES, verbose_name="Issue addressed?")

    bug_created_date = models.DateTimeField(auto_now_add=True)

    bug_published_date = models.DateTimeField(blank=True, null=True)

    bug_update_date = models.DateTimeField(auto_now=True, verbose_name="Last Updated:")

 

    class Meta:

        verbose_name_plural="Bugs"

       

 

    def publish(self):

        self.published_date = timezone.now()

        self.save()

 

    def approve_comments(self):

        return self.comments.filter(approved_comment=True)

 

    def get_absolute_url(self):

        print(self.pk)

        return reverse("bugs/bug_detail",kwargs={'pk':self.pk})

 

    def __str__(self):

        return self.bug_title

 

 

Thanks for your attention to date.

 

Bruckner de Villiers

083 625 1086

 

--

You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.

Jason

unread,
Feb 10, 2020, 7:54:12 AM2/10/20
to Django users
Hmm.  TIL about https://code.djangoproject.com/ticket/8576

Would a a uuid for this, rather than an integer?  If not, you'll have to implement a save override to handle the incrementing yourself.

In addition, it might be worthwhile bringing this ticket up for discussion at https://groups.google.com/forum/#!forum/django-developers
Reply all
Reply to author
Forward
0 new messages