django-rest-framework raises an error in a model when accessing another model with a foreign key










0














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)
AttributeError: 'NoneType' object has no attribute 'id'
, and this error is raised on method __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










share|improve this question























  • 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






  • 1




    You'd defined contract as null=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















0














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)
AttributeError: 'NoneType' object has no attribute 'id'
, and this error is raised on method __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










share|improve this question























  • 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






  • 1




    You'd defined contract as null=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













0












0








0







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)
AttributeError: 'NoneType' object has no attribute 'id'
, and this error is raised on method __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










share|improve this question















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)
AttributeError: 'NoneType' object has no attribute 'id'
, and this error is raised on method __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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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

359113




359113











  • 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






  • 1




    You'd defined contract as null=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










  • 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 as null=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












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
);



);













draft saved

draft discarded


















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















draft saved

draft discarded
















































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.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • 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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

Top Tejano songwriter Luis Silva dead of heart attack at 64

政党

天津地下鉄3号線