Autoincrementing field on object save - DjangoExtending the User model with custom fields in DjangoDjango foreign key access in save() functionDoes Django scale?Does get_or_create() have to save right away? (Django)Need a minimal Django file upload exampledifferentiate null=True, blank=True in djangoDjango-Rest-Framework - How to serialize queryset from an unrelated model as nested serializerHow to set dynamic initial values to django modelform fieldHow to edit requested params in Django Admin Page?How to implement update_or_create inside create method of ModelSerializer
Is there any direct train from LHR Airport to Newcastle Gateshead?
As a DM of a 4-player group, would it be appropriate for me to run a private 1-on-1 session so that one PC can act secretly?
Has Peter Parker ever eaten bugs?
How can I disable a reserved profile?
Why didn't NASA launch communications relay satellites for the Apollo missions?
What kind of curve (or model) should I fit to my percentage data?
Is it better to deliver many low-value stories or few high-value stories?
Caption in landscape table, need help?
Pass USB 3.0 connection through D-SUB connector
What does a black-and-white Puerto Rican flag signify?
Help!. Wiring an IKEA light fixture into old fixture
How can I deal with someone that wants to kill something that isn't supposed to be killed?
Why is DC so, so, so Democratic?
90s (or earlier) book in which a human detective is hired by a vampire to find killer of vampires
Why did modems have speakers?
Recursive search on Node Tree with Linq and Queue
is FIND WORDS in P?
I am a dual citizen of United States and Mexico, can I use my Mexican license in california when visiting?
Does switching on an old games console without a cartridge damage it?
Found old paper shares of Motorola Inc that has since been broken up
Storyboarding Approaches for the Non-Artistic
Can someone clarify when a Trigger executes on a single record vs. multiple in one context?
Does Mathematica 12 support GT 730 CUDA?
Calculating Fibonacci sequence in several different ways
Autoincrementing field on object save - Django
Extending the User model with custom fields in DjangoDjango foreign key access in save() functionDoes Django scale?Does get_or_create() have to save right away? (Django)Need a minimal Django file upload exampledifferentiate null=True, blank=True in djangoDjango-Rest-Framework - How to serialize queryset from an unrelated model as nested serializerHow to set dynamic initial values to django modelform fieldHow to edit requested params in Django Admin Page?How to implement update_or_create inside create method of ModelSerializer
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
So far, I have this function:
def my_number():
x = '0000'
return 'MY0'.format(str(int(x) + 1).zfill(len(x)))
Which I call like this:
number_seq = models.CharField(_("Ref."), unique=True, db_index=True,
default=my_number, null=True)
It works, but when I try to save a new document, it just creates a new MY0001
, and it should be incrementing, like MY0002, MY0003
and so forth.
What might be the reason for that?
django python-3.x
add a comment |
So far, I have this function:
def my_number():
x = '0000'
return 'MY0'.format(str(int(x) + 1).zfill(len(x)))
Which I call like this:
number_seq = models.CharField(_("Ref."), unique=True, db_index=True,
default=my_number, null=True)
It works, but when I try to save a new document, it just creates a new MY0001
, and it should be incrementing, like MY0002, MY0003
and so forth.
What might be the reason for that?
django python-3.x
Well, you are using a variable from the local scope you are setting to 0. Why would you expect it to change?
– Alvaro
Mar 26 at 13:37
I tried like = x = 1 return 'MY0'.format(str(int(x) + 1).zfill(4)) But it's stuck on MY0002 now, I'm a bit confused
– NeoVe
Mar 26 at 13:47
add a comment |
So far, I have this function:
def my_number():
x = '0000'
return 'MY0'.format(str(int(x) + 1).zfill(len(x)))
Which I call like this:
number_seq = models.CharField(_("Ref."), unique=True, db_index=True,
default=my_number, null=True)
It works, but when I try to save a new document, it just creates a new MY0001
, and it should be incrementing, like MY0002, MY0003
and so forth.
What might be the reason for that?
django python-3.x
So far, I have this function:
def my_number():
x = '0000'
return 'MY0'.format(str(int(x) + 1).zfill(len(x)))
Which I call like this:
number_seq = models.CharField(_("Ref."), unique=True, db_index=True,
default=my_number, null=True)
It works, but when I try to save a new document, it just creates a new MY0001
, and it should be incrementing, like MY0002, MY0003
and so forth.
What might be the reason for that?
django python-3.x
django python-3.x
asked Mar 26 at 13:34
NeoVeNeoVe
2,0766 gold badges33 silver badges81 bronze badges
2,0766 gold badges33 silver badges81 bronze badges
Well, you are using a variable from the local scope you are setting to 0. Why would you expect it to change?
– Alvaro
Mar 26 at 13:37
I tried like = x = 1 return 'MY0'.format(str(int(x) + 1).zfill(4)) But it's stuck on MY0002 now, I'm a bit confused
– NeoVe
Mar 26 at 13:47
add a comment |
Well, you are using a variable from the local scope you are setting to 0. Why would you expect it to change?
– Alvaro
Mar 26 at 13:37
I tried like = x = 1 return 'MY0'.format(str(int(x) + 1).zfill(4)) But it's stuck on MY0002 now, I'm a bit confused
– NeoVe
Mar 26 at 13:47
Well, you are using a variable from the local scope you are setting to 0. Why would you expect it to change?
– Alvaro
Mar 26 at 13:37
Well, you are using a variable from the local scope you are setting to 0. Why would you expect it to change?
– Alvaro
Mar 26 at 13:37
I tried like = x = 1 return 'MY0'.format(str(int(x) + 1).zfill(4)) But it's stuck on MY0002 now, I'm a bit confused
– NeoVe
Mar 26 at 13:47
I tried like = x = 1 return 'MY0'.format(str(int(x) + 1).zfill(4)) But it's stuck on MY0002 now, I'm a bit confused
– NeoVe
Mar 26 at 13:47
add a comment |
1 Answer
1
active
oldest
votes
Rather than taking your approach, I think you should declare a AutoField
as primary key, and maybe have a property method named number_seq
to get the value for reference. For example:
class SomeModel(models.Model):
your_pk = models.AutoField(primary_key=True)
@property
def number_seq(self):
return 'MY0'.format(str(self.your_pk).zfill(4))
But you can't run query against it directly, like SomeModel.objects.filter(number_seq="MY001")
will not work. But you can do the following to get the needed objects. For example:
value = "MY001"
SomeModel.objects.get(pk=int(value[2:]))
Hi, thank you very much, sounds good, but most of my child models will need to have this property now, I don't know if it will be safe with this approach
– NeoVe
Mar 26 at 14:01
Sorry i didn't get what you meant with what kind of safety you are referring to
– ruddra
Mar 26 at 14:13
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55358490%2fautoincrementing-field-on-object-save-django%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Rather than taking your approach, I think you should declare a AutoField
as primary key, and maybe have a property method named number_seq
to get the value for reference. For example:
class SomeModel(models.Model):
your_pk = models.AutoField(primary_key=True)
@property
def number_seq(self):
return 'MY0'.format(str(self.your_pk).zfill(4))
But you can't run query against it directly, like SomeModel.objects.filter(number_seq="MY001")
will not work. But you can do the following to get the needed objects. For example:
value = "MY001"
SomeModel.objects.get(pk=int(value[2:]))
Hi, thank you very much, sounds good, but most of my child models will need to have this property now, I don't know if it will be safe with this approach
– NeoVe
Mar 26 at 14:01
Sorry i didn't get what you meant with what kind of safety you are referring to
– ruddra
Mar 26 at 14:13
add a comment |
Rather than taking your approach, I think you should declare a AutoField
as primary key, and maybe have a property method named number_seq
to get the value for reference. For example:
class SomeModel(models.Model):
your_pk = models.AutoField(primary_key=True)
@property
def number_seq(self):
return 'MY0'.format(str(self.your_pk).zfill(4))
But you can't run query against it directly, like SomeModel.objects.filter(number_seq="MY001")
will not work. But you can do the following to get the needed objects. For example:
value = "MY001"
SomeModel.objects.get(pk=int(value[2:]))
Hi, thank you very much, sounds good, but most of my child models will need to have this property now, I don't know if it will be safe with this approach
– NeoVe
Mar 26 at 14:01
Sorry i didn't get what you meant with what kind of safety you are referring to
– ruddra
Mar 26 at 14:13
add a comment |
Rather than taking your approach, I think you should declare a AutoField
as primary key, and maybe have a property method named number_seq
to get the value for reference. For example:
class SomeModel(models.Model):
your_pk = models.AutoField(primary_key=True)
@property
def number_seq(self):
return 'MY0'.format(str(self.your_pk).zfill(4))
But you can't run query against it directly, like SomeModel.objects.filter(number_seq="MY001")
will not work. But you can do the following to get the needed objects. For example:
value = "MY001"
SomeModel.objects.get(pk=int(value[2:]))
Rather than taking your approach, I think you should declare a AutoField
as primary key, and maybe have a property method named number_seq
to get the value for reference. For example:
class SomeModel(models.Model):
your_pk = models.AutoField(primary_key=True)
@property
def number_seq(self):
return 'MY0'.format(str(self.your_pk).zfill(4))
But you can't run query against it directly, like SomeModel.objects.filter(number_seq="MY001")
will not work. But you can do the following to get the needed objects. For example:
value = "MY001"
SomeModel.objects.get(pk=int(value[2:]))
answered Mar 26 at 13:50
ruddraruddra
21k5 gold badges31 silver badges53 bronze badges
21k5 gold badges31 silver badges53 bronze badges
Hi, thank you very much, sounds good, but most of my child models will need to have this property now, I don't know if it will be safe with this approach
– NeoVe
Mar 26 at 14:01
Sorry i didn't get what you meant with what kind of safety you are referring to
– ruddra
Mar 26 at 14:13
add a comment |
Hi, thank you very much, sounds good, but most of my child models will need to have this property now, I don't know if it will be safe with this approach
– NeoVe
Mar 26 at 14:01
Sorry i didn't get what you meant with what kind of safety you are referring to
– ruddra
Mar 26 at 14:13
Hi, thank you very much, sounds good, but most of my child models will need to have this property now, I don't know if it will be safe with this approach
– NeoVe
Mar 26 at 14:01
Hi, thank you very much, sounds good, but most of my child models will need to have this property now, I don't know if it will be safe with this approach
– NeoVe
Mar 26 at 14:01
Sorry i didn't get what you meant with what kind of safety you are referring to
– ruddra
Mar 26 at 14:13
Sorry i didn't get what you meant with what kind of safety you are referring to
– ruddra
Mar 26 at 14:13
add a comment |
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55358490%2fautoincrementing-field-on-object-save-django%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Well, you are using a variable from the local scope you are setting to 0. Why would you expect it to change?
– Alvaro
Mar 26 at 13:37
I tried like = x = 1 return 'MY0'.format(str(int(x) + 1).zfill(4)) But it's stuck on MY0002 now, I'm a bit confused
– NeoVe
Mar 26 at 13:47