django-rest-framework raises an error in a model when accessing another model with a foreign key
I have two models, one referring the other one:
class RatesTable(models.Model):
hotel = models.ForeignKey(Hotel, on_delete=models.PROTECT, related_name='rates_tables', verbose_name=_('Hotel'))
contract = models.ForeignKey(Contract, on_delete=models.PROTECT, null=True, blank=True, related_name='rates_tables', verbose_name=_('Contract'))
name = models.CharField(max_length=100, verbose_name=_('Name'))
description = models.TextField(null=True, blank=True, verbose_name=_('Description'))
is_base = models.BooleanField(verbose_name=_('Is a base rate-table?'))
start_date = models.DateField(null=True, blank=True, verbose_name=_('Start Date'))
due_date = models.DateField(null=True, blank=True, verbose_name=_('Due Date'))
release = models.IntegerField(verbose_name=_('Release'))
class Meta:
verbose_name = _('Rates Table')
verbose_name_plural = _('Rates Tables')
unique_together = ('contract', 'start_date', 'due_date')
indexes = [
models.Index(fields=['hotel',]),
]
def __str__(self):
return "[{}][{}]{} {} {}-{}".format(self.id, self.hotel.name, self.contract.id, self.name, self.start_date, self.due_date)
class Rate(models.Model):
rates_table = models.ForeignKey(RatesTable, on_delete=models.PROTECT, related_name='rates', verbose_name=_('Rates Table'))
product = models.ForeignKey(Product, on_delete=models.PROTECT, related_name='product_rates', verbose_name=_('Product'))
rate = models.FloatField(verbose_name=_('Rate'))
class Meta:
verbose_name = _('Rate')
verbose_name_plural = _('Rates')
unique_together = ('rates_table', 'product')
def __str__(self):
return "[{}][{}][{}]{}".format(self.id, self.rates_table.contract, self.rates_table.id, self.product.name)
Each one has its own standard end-point in urls.py
. End-point for model RatesTable
works perfectly, but end-point for model Rate
raises this error: return "[{}][{}]{} {} {}-{}".format(self.id, self.hotel.name, self.contract.id, self.name, self.start_date, self.due_date)
, and this error is raised on method
AttributeError: 'NoneType' object has no attribute 'id'__str__(self)
of model RatesTable
, not model Rate
!
I don't know why DRF raises an error in the referenced model. It's the only case it happens, and I have many models with foreign keys
django django-models django-rest-framework
add a comment |
I have two models, one referring the other one:
class RatesTable(models.Model):
hotel = models.ForeignKey(Hotel, on_delete=models.PROTECT, related_name='rates_tables', verbose_name=_('Hotel'))
contract = models.ForeignKey(Contract, on_delete=models.PROTECT, null=True, blank=True, related_name='rates_tables', verbose_name=_('Contract'))
name = models.CharField(max_length=100, verbose_name=_('Name'))
description = models.TextField(null=True, blank=True, verbose_name=_('Description'))
is_base = models.BooleanField(verbose_name=_('Is a base rate-table?'))
start_date = models.DateField(null=True, blank=True, verbose_name=_('Start Date'))
due_date = models.DateField(null=True, blank=True, verbose_name=_('Due Date'))
release = models.IntegerField(verbose_name=_('Release'))
class Meta:
verbose_name = _('Rates Table')
verbose_name_plural = _('Rates Tables')
unique_together = ('contract', 'start_date', 'due_date')
indexes = [
models.Index(fields=['hotel',]),
]
def __str__(self):
return "[{}][{}]{} {} {}-{}".format(self.id, self.hotel.name, self.contract.id, self.name, self.start_date, self.due_date)
class Rate(models.Model):
rates_table = models.ForeignKey(RatesTable, on_delete=models.PROTECT, related_name='rates', verbose_name=_('Rates Table'))
product = models.ForeignKey(Product, on_delete=models.PROTECT, related_name='product_rates', verbose_name=_('Product'))
rate = models.FloatField(verbose_name=_('Rate'))
class Meta:
verbose_name = _('Rate')
verbose_name_plural = _('Rates')
unique_together = ('rates_table', 'product')
def __str__(self):
return "[{}][{}][{}]{}".format(self.id, self.rates_table.contract, self.rates_table.id, self.product.name)
Each one has its own standard end-point in urls.py
. End-point for model RatesTable
works perfectly, but end-point for model Rate
raises this error: return "[{}][{}]{} {} {}-{}".format(self.id, self.hotel.name, self.contract.id, self.name, self.start_date, self.due_date)
, and this error is raised on method
AttributeError: 'NoneType' object has no attribute 'id'__str__(self)
of model RatesTable
, not model Rate
!
I don't know why DRF raises an error in the referenced model. It's the only case it happens, and I have many models with foreign keys
django django-models django-rest-framework
yourself.rates_table
relation may beNone
– JPG
Nov 13 '18 at 5:45
Nope, it is not the case. I don't have any record with a null value in the foreign key column.
– Hugo Luis Villalobos Canto
Nov 13 '18 at 5:50
1
You'd definedcontract
asnull=True
, it may be the issue
– JPG
Nov 13 '18 at 5:56
1
@JPG You are right!!! it was so.
– Hugo Luis Villalobos Canto
Nov 13 '18 at 17:10
add a comment |
I have two models, one referring the other one:
class RatesTable(models.Model):
hotel = models.ForeignKey(Hotel, on_delete=models.PROTECT, related_name='rates_tables', verbose_name=_('Hotel'))
contract = models.ForeignKey(Contract, on_delete=models.PROTECT, null=True, blank=True, related_name='rates_tables', verbose_name=_('Contract'))
name = models.CharField(max_length=100, verbose_name=_('Name'))
description = models.TextField(null=True, blank=True, verbose_name=_('Description'))
is_base = models.BooleanField(verbose_name=_('Is a base rate-table?'))
start_date = models.DateField(null=True, blank=True, verbose_name=_('Start Date'))
due_date = models.DateField(null=True, blank=True, verbose_name=_('Due Date'))
release = models.IntegerField(verbose_name=_('Release'))
class Meta:
verbose_name = _('Rates Table')
verbose_name_plural = _('Rates Tables')
unique_together = ('contract', 'start_date', 'due_date')
indexes = [
models.Index(fields=['hotel',]),
]
def __str__(self):
return "[{}][{}]{} {} {}-{}".format(self.id, self.hotel.name, self.contract.id, self.name, self.start_date, self.due_date)
class Rate(models.Model):
rates_table = models.ForeignKey(RatesTable, on_delete=models.PROTECT, related_name='rates', verbose_name=_('Rates Table'))
product = models.ForeignKey(Product, on_delete=models.PROTECT, related_name='product_rates', verbose_name=_('Product'))
rate = models.FloatField(verbose_name=_('Rate'))
class Meta:
verbose_name = _('Rate')
verbose_name_plural = _('Rates')
unique_together = ('rates_table', 'product')
def __str__(self):
return "[{}][{}][{}]{}".format(self.id, self.rates_table.contract, self.rates_table.id, self.product.name)
Each one has its own standard end-point in urls.py
. End-point for model RatesTable
works perfectly, but end-point for model Rate
raises this error: return "[{}][{}]{} {} {}-{}".format(self.id, self.hotel.name, self.contract.id, self.name, self.start_date, self.due_date)
, and this error is raised on method
AttributeError: 'NoneType' object has no attribute 'id'__str__(self)
of model RatesTable
, not model Rate
!
I don't know why DRF raises an error in the referenced model. It's the only case it happens, and I have many models with foreign keys
django django-models django-rest-framework
I have two models, one referring the other one:
class RatesTable(models.Model):
hotel = models.ForeignKey(Hotel, on_delete=models.PROTECT, related_name='rates_tables', verbose_name=_('Hotel'))
contract = models.ForeignKey(Contract, on_delete=models.PROTECT, null=True, blank=True, related_name='rates_tables', verbose_name=_('Contract'))
name = models.CharField(max_length=100, verbose_name=_('Name'))
description = models.TextField(null=True, blank=True, verbose_name=_('Description'))
is_base = models.BooleanField(verbose_name=_('Is a base rate-table?'))
start_date = models.DateField(null=True, blank=True, verbose_name=_('Start Date'))
due_date = models.DateField(null=True, blank=True, verbose_name=_('Due Date'))
release = models.IntegerField(verbose_name=_('Release'))
class Meta:
verbose_name = _('Rates Table')
verbose_name_plural = _('Rates Tables')
unique_together = ('contract', 'start_date', 'due_date')
indexes = [
models.Index(fields=['hotel',]),
]
def __str__(self):
return "[{}][{}]{} {} {}-{}".format(self.id, self.hotel.name, self.contract.id, self.name, self.start_date, self.due_date)
class Rate(models.Model):
rates_table = models.ForeignKey(RatesTable, on_delete=models.PROTECT, related_name='rates', verbose_name=_('Rates Table'))
product = models.ForeignKey(Product, on_delete=models.PROTECT, related_name='product_rates', verbose_name=_('Product'))
rate = models.FloatField(verbose_name=_('Rate'))
class Meta:
verbose_name = _('Rate')
verbose_name_plural = _('Rates')
unique_together = ('rates_table', 'product')
def __str__(self):
return "[{}][{}][{}]{}".format(self.id, self.rates_table.contract, self.rates_table.id, self.product.name)
Each one has its own standard end-point in urls.py
. End-point for model RatesTable
works perfectly, but end-point for model Rate
raises this error: return "[{}][{}]{} {} {}-{}".format(self.id, self.hotel.name, self.contract.id, self.name, self.start_date, self.due_date)
, and this error is raised on method
AttributeError: 'NoneType' object has no attribute 'id'__str__(self)
of model RatesTable
, not model Rate
!
I don't know why DRF raises an error in the referenced model. It's the only case it happens, and I have many models with foreign keys
django django-models django-rest-framework
django django-models django-rest-framework
edited Nov 13 '18 at 5:45
Hugo Luis Villalobos Canto
asked Nov 13 '18 at 5:44
Hugo Luis Villalobos CantoHugo Luis Villalobos Canto
540214
540214
yourself.rates_table
relation may beNone
– JPG
Nov 13 '18 at 5:45
Nope, it is not the case. I don't have any record with a null value in the foreign key column.
– Hugo Luis Villalobos Canto
Nov 13 '18 at 5:50
1
You'd definedcontract
asnull=True
, it may be the issue
– JPG
Nov 13 '18 at 5:56
1
@JPG You are right!!! it was so.
– Hugo Luis Villalobos Canto
Nov 13 '18 at 17:10
add a comment |
yourself.rates_table
relation may beNone
– JPG
Nov 13 '18 at 5:45
Nope, it is not the case. I don't have any record with a null value in the foreign key column.
– Hugo Luis Villalobos Canto
Nov 13 '18 at 5:50
1
You'd definedcontract
asnull=True
, it may be the issue
– JPG
Nov 13 '18 at 5:56
1
@JPG You are right!!! it was so.
– Hugo Luis Villalobos Canto
Nov 13 '18 at 17:10
your
self.rates_table
relation may be None
– JPG
Nov 13 '18 at 5:45
your
self.rates_table
relation may be None
– JPG
Nov 13 '18 at 5:45
Nope, it is not the case. I don't have any record with a null value in the foreign key column.
– Hugo Luis Villalobos Canto
Nov 13 '18 at 5:50
Nope, it is not the case. I don't have any record with a null value in the foreign key column.
– Hugo Luis Villalobos Canto
Nov 13 '18 at 5:50
1
1
You'd defined
contract
as null=True
, it may be the issue– JPG
Nov 13 '18 at 5:56
You'd defined
contract
as null=True
, it may be the issue– JPG
Nov 13 '18 at 5:56
1
1
@JPG You are right!!! it was so.
– Hugo Luis Villalobos Canto
Nov 13 '18 at 17:10
@JPG You are right!!! it was so.
– Hugo Luis Villalobos Canto
Nov 13 '18 at 17:10
add a comment |
0
active
oldest
votes
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%2f53274551%2fdjango-rest-framework-raises-an-error-in-a-model-when-accessing-another-model-wi%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
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%2f53274551%2fdjango-rest-framework-raises-an-error-in-a-model-when-accessing-another-model-wi%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
your
self.rates_table
relation may beNone
– JPG
Nov 13 '18 at 5:45
Nope, it is not the case. I don't have any record with a null value in the foreign key column.
– Hugo Luis Villalobos Canto
Nov 13 '18 at 5:50
1
You'd defined
contract
asnull=True
, it may be the issue– JPG
Nov 13 '18 at 5:56
1
@JPG You are right!!! it was so.
– Hugo Luis Villalobos Canto
Nov 13 '18 at 17:10