Set the default billing address in admin form
I'm working on a Django project where I have several customers, each of which can have multiple postal addresses (office 1, office 2, office 3, ...).
I have to choose the default address for each customer.
I created a model for postal addresses with ForeignKey pointing to the customer model and in my customer model I entered a PositiveIntegerField to contain the default postal address ID.
The problem is that I do not know how to filter in the Admin Form only the addresses relevant to the client and show the description of the address instead of a PositiveIntegerField.
Some advice?
python django django-models django-admin
add a comment |
I'm working on a Django project where I have several customers, each of which can have multiple postal addresses (office 1, office 2, office 3, ...).
I have to choose the default address for each customer.
I created a model for postal addresses with ForeignKey pointing to the customer model and in my customer model I entered a PositiveIntegerField to contain the default postal address ID.
The problem is that I do not know how to filter in the Admin Form only the addresses relevant to the client and show the description of the address instead of a PositiveIntegerField.
Some advice?
python django django-models django-admin
1
Don't use aPositiveIntegerField
! Use aForeignKey
. You can even limit the queryset of options for thatForeignKey
to addresses that "belong" to that client.
– Willem Van Onsem
Jan 18 at 18:56
add a comment |
I'm working on a Django project where I have several customers, each of which can have multiple postal addresses (office 1, office 2, office 3, ...).
I have to choose the default address for each customer.
I created a model for postal addresses with ForeignKey pointing to the customer model and in my customer model I entered a PositiveIntegerField to contain the default postal address ID.
The problem is that I do not know how to filter in the Admin Form only the addresses relevant to the client and show the description of the address instead of a PositiveIntegerField.
Some advice?
python django django-models django-admin
I'm working on a Django project where I have several customers, each of which can have multiple postal addresses (office 1, office 2, office 3, ...).
I have to choose the default address for each customer.
I created a model for postal addresses with ForeignKey pointing to the customer model and in my customer model I entered a PositiveIntegerField to contain the default postal address ID.
The problem is that I do not know how to filter in the Admin Form only the addresses relevant to the client and show the description of the address instead of a PositiveIntegerField.
Some advice?
python django django-models django-admin
python django django-models django-admin
asked Jan 18 at 18:55
Filippo RuggeriFilippo Ruggeri
82
82
1
Don't use aPositiveIntegerField
! Use aForeignKey
. You can even limit the queryset of options for thatForeignKey
to addresses that "belong" to that client.
– Willem Van Onsem
Jan 18 at 18:56
add a comment |
1
Don't use aPositiveIntegerField
! Use aForeignKey
. You can even limit the queryset of options for thatForeignKey
to addresses that "belong" to that client.
– Willem Van Onsem
Jan 18 at 18:56
1
1
Don't use a
PositiveIntegerField
! Use a ForeignKey
. You can even limit the queryset of options for that ForeignKey
to addresses that "belong" to that client.– Willem Van Onsem
Jan 18 at 18:56
Don't use a
PositiveIntegerField
! Use a ForeignKey
. You can even limit the queryset of options for that ForeignKey
to addresses that "belong" to that client.– Willem Van Onsem
Jan 18 at 18:56
add a comment |
1 Answer
1
active
oldest
votes
I created a model for postal addresses with
ForeignKey
pointing to the customer model and in my customer model I entered aPositiveIntegerField
to contain the default postal address ID.
Don't do that. This is in fact a relation, so you should use the tool that is constructed for this: a ForeignKey
[Django-doc].
We can thus construct something like:
class Customer(models.Model):
default_address = models.ForeignKey(
'app.Address',
on_delete=models.SET_NULL,
null=True
)
class Address(models.Model):
customer = models.ForeignKey(
Customer,
on_delete=models.CASCADE,
related_name='addresses'
)
In a ModelForm
you can then restrict the options to addresses that belong to that specific instance. For example:
class CustomerModelForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super(CustomerModelForm, self).__init__(*args, **kwargs)
if self.instance:
self.fields['default_address'].queryset = Address.objects.filter(
customer=self.instance
)
class Meta:
model = Customer
So here we limit the options to addresses that belong to the customer.
The advantage of using a ForeignKey
is that the database will protect referential integrity (the id can not refer to a non-existing address), and furthermore all advanced querying you can do with Django's ORM is still possible here. You can thus filter on Customer
s with a default_address
that is located in London for example with something like Customer.objects.filter(default_address__city='London')
(given an address has a city
field).
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%2f54259921%2fset-the-default-billing-address-in-admin-form%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
I created a model for postal addresses with
ForeignKey
pointing to the customer model and in my customer model I entered aPositiveIntegerField
to contain the default postal address ID.
Don't do that. This is in fact a relation, so you should use the tool that is constructed for this: a ForeignKey
[Django-doc].
We can thus construct something like:
class Customer(models.Model):
default_address = models.ForeignKey(
'app.Address',
on_delete=models.SET_NULL,
null=True
)
class Address(models.Model):
customer = models.ForeignKey(
Customer,
on_delete=models.CASCADE,
related_name='addresses'
)
In a ModelForm
you can then restrict the options to addresses that belong to that specific instance. For example:
class CustomerModelForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super(CustomerModelForm, self).__init__(*args, **kwargs)
if self.instance:
self.fields['default_address'].queryset = Address.objects.filter(
customer=self.instance
)
class Meta:
model = Customer
So here we limit the options to addresses that belong to the customer.
The advantage of using a ForeignKey
is that the database will protect referential integrity (the id can not refer to a non-existing address), and furthermore all advanced querying you can do with Django's ORM is still possible here. You can thus filter on Customer
s with a default_address
that is located in London for example with something like Customer.objects.filter(default_address__city='London')
(given an address has a city
field).
add a comment |
I created a model for postal addresses with
ForeignKey
pointing to the customer model and in my customer model I entered aPositiveIntegerField
to contain the default postal address ID.
Don't do that. This is in fact a relation, so you should use the tool that is constructed for this: a ForeignKey
[Django-doc].
We can thus construct something like:
class Customer(models.Model):
default_address = models.ForeignKey(
'app.Address',
on_delete=models.SET_NULL,
null=True
)
class Address(models.Model):
customer = models.ForeignKey(
Customer,
on_delete=models.CASCADE,
related_name='addresses'
)
In a ModelForm
you can then restrict the options to addresses that belong to that specific instance. For example:
class CustomerModelForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super(CustomerModelForm, self).__init__(*args, **kwargs)
if self.instance:
self.fields['default_address'].queryset = Address.objects.filter(
customer=self.instance
)
class Meta:
model = Customer
So here we limit the options to addresses that belong to the customer.
The advantage of using a ForeignKey
is that the database will protect referential integrity (the id can not refer to a non-existing address), and furthermore all advanced querying you can do with Django's ORM is still possible here. You can thus filter on Customer
s with a default_address
that is located in London for example with something like Customer.objects.filter(default_address__city='London')
(given an address has a city
field).
add a comment |
I created a model for postal addresses with
ForeignKey
pointing to the customer model and in my customer model I entered aPositiveIntegerField
to contain the default postal address ID.
Don't do that. This is in fact a relation, so you should use the tool that is constructed for this: a ForeignKey
[Django-doc].
We can thus construct something like:
class Customer(models.Model):
default_address = models.ForeignKey(
'app.Address',
on_delete=models.SET_NULL,
null=True
)
class Address(models.Model):
customer = models.ForeignKey(
Customer,
on_delete=models.CASCADE,
related_name='addresses'
)
In a ModelForm
you can then restrict the options to addresses that belong to that specific instance. For example:
class CustomerModelForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super(CustomerModelForm, self).__init__(*args, **kwargs)
if self.instance:
self.fields['default_address'].queryset = Address.objects.filter(
customer=self.instance
)
class Meta:
model = Customer
So here we limit the options to addresses that belong to the customer.
The advantage of using a ForeignKey
is that the database will protect referential integrity (the id can not refer to a non-existing address), and furthermore all advanced querying you can do with Django's ORM is still possible here. You can thus filter on Customer
s with a default_address
that is located in London for example with something like Customer.objects.filter(default_address__city='London')
(given an address has a city
field).
I created a model for postal addresses with
ForeignKey
pointing to the customer model and in my customer model I entered aPositiveIntegerField
to contain the default postal address ID.
Don't do that. This is in fact a relation, so you should use the tool that is constructed for this: a ForeignKey
[Django-doc].
We can thus construct something like:
class Customer(models.Model):
default_address = models.ForeignKey(
'app.Address',
on_delete=models.SET_NULL,
null=True
)
class Address(models.Model):
customer = models.ForeignKey(
Customer,
on_delete=models.CASCADE,
related_name='addresses'
)
In a ModelForm
you can then restrict the options to addresses that belong to that specific instance. For example:
class CustomerModelForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super(CustomerModelForm, self).__init__(*args, **kwargs)
if self.instance:
self.fields['default_address'].queryset = Address.objects.filter(
customer=self.instance
)
class Meta:
model = Customer
So here we limit the options to addresses that belong to the customer.
The advantage of using a ForeignKey
is that the database will protect referential integrity (the id can not refer to a non-existing address), and furthermore all advanced querying you can do with Django's ORM is still possible here. You can thus filter on Customer
s with a default_address
that is located in London for example with something like Customer.objects.filter(default_address__city='London')
(given an address has a city
field).
answered Jan 18 at 19:05
Willem Van OnsemWillem Van Onsem
146k16141230
146k16141230
add a comment |
add a comment |
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%2f54259921%2fset-the-default-billing-address-in-admin-form%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
1
Don't use a
PositiveIntegerField
! Use aForeignKey
. You can even limit the queryset of options for thatForeignKey
to addresses that "belong" to that client.– Willem Van Onsem
Jan 18 at 18:56