Wednesday, June 25, 2014

Re: Weird error like tickets 8892 and 10811

ergh. Rookie error. Subtle bug in the Job class - see the save override:

def save(self, *args, **kwargs):
if not self.slug:
self.slug = self.job_number
super(Job, self).save(*args, **kwargs)


Note that the call to super is only reached....when there is no slug.
The fix is simple:

def save(self, *args, **kwargs):
if not self.slug:
self.slug = self.job_number
super(Job, self).save(*args, **kwargs)


cheers
L.

On 26 June 2014 14:09, Lachlan Musicman <datakid@gmail.com> wrote:
> Ok, I now understand why this happens:
>
>>>> x == j
> True
>>>> j == x
> True
>>>> j.findings
> 'data'
>>>> x.findings
> u''
>
> https://docs.djangoproject.com/en/dev/ref/models/instances/#eq
>
> "with the same primary key value and the same concrete class are
> considered equal. "
>
> (no docs in 1.6, but I presume that was oversight rather than a
> change, since no changes are referenced in the docs.)
>
> cheers.
> L.
>
> On 26 June 2014 13:24, Lachlan Musicman <datakid@gmail.com> wrote:
>> I have run up against this bug before:
>>
>> https://code.djangoproject.com/ticket/8892
>>
>> Recently, it was updated to "duplicate of"
>>
>> https://code.djangoproject.com/ticket/10811
>>
>> The TL;DR is
>>
>> Sometimes when you have models with O2O or FK you can lose data if the
>> models are saved in the wrong order.
>>
>> I'm having a similar yet different issue, using Django 1.6.5
>>
>> I have a Job model with subclasses
>>
>> class Job(models.Model):
>> job_number = models.CharField("Job Number", max_length=20,
>> unique=True, db_index=True)
>> date_opened = models.DateField()
>> slug = models.SlugField(max_length=20, unique=True)
>> def save(self, *args, **kwargs):
>> if not self.slug:
>> self.slug = self.job_number
>> super(Job, self).save(*args, **kwargs)
>>
>> class WorkshopJob(Job)
>> job = models.OneToOneField(Job, parent_link=True)
>> invoice_number = models.CharField("Invoice Number", max_length=30, blank=True)
>> reported_fault = models.TextField()
>>
>>
>> And here we see a similar result:
>>
>>>>> j = WorkshopJob.objects.get(id=9)
>>>>> j
>> <WorkshopJob: 1234>
>>>>> j.date_opened
>> datetime.date(2014, 6, 26)
>>>>> j.invoice_number
>> u'123'
>>>>> j.findings
>> u''
>>>>> j.findings="The CPU doesn't work"
>>>>> j.findings
>> "The CPU doesn't work"
>>>>> j.save()
>>>>> j.findings
>> "The CPU doesn't work"
>>>>> j = WorkshopJob.objects.get(id=10)
>>>>> j.findings
>> u''
>>>>> j = WorkshopJob.objects.get(id=9)
>>>>> j.findings
>> u''
>>>>>
>>
>>
>> Is this expected behaviour?
>>
>> Is this caused by having overridden the save method on the Job class
>> and then not following up with an overridden save method on
>> WorkshopJob?
>>
>> As another example:
>>
>>>>> j = WorkshopCivilJob.objects.get(id=9)
>>>>> j.findings
>> u''
>>>>> j.findings="data"
>>>>> j.findings
>> 'data'
>>>>> j.save()
>>>>> j.findings
>> 'data'
>>>>> x = WorkshopCivilJob.objects.get(id=9)
>>>>> x.findings
>> u''
>>>>> x == j
>> True
>>>>> j == x
>> True
>>>>> j.findings
>> 'data'
>>>>> x.findings
>> u''
>>>>>
>>
>> Now my brain hurts.
>>
>> How can I fix this?
>>
>> cheers
>> L.
>>
>>
>> --
>> The idea is that a beautiful image is frameable. Everything you need
>> to see is there: It's everything you want, and it's very pleasing
>> because there's no extra information that you don't get to see.
>> Everything's in a nice package for you. But sublime art is
>> unframeable: It's an image or idea that implies that there's a bigger
>> image or idea that you can't see: You're only getting to look at a
>> fraction of it, and in that way it's both beautiful and scary, because
>> it's reminding you that there's more that you don't have access to.
>> It's now sort of left the piece itself and it's become your own
>> invention, so it's personal as well as being scary as well as being
>> beautiful, which is what I really like about art like that.
>> -----------------------------------------------------------------------------------------------------------
>> Adventure Time http://theholenearthecenteroftheworld.com/
>
>
>
> --
> The idea is that a beautiful image is frameable. Everything you need
> to see is there: It's everything you want, and it's very pleasing
> because there's no extra information that you don't get to see.
> Everything's in a nice package for you. But sublime art is
> unframeable: It's an image or idea that implies that there's a bigger
> image or idea that you can't see: You're only getting to look at a
> fraction of it, and in that way it's both beautiful and scary, because
> it's reminding you that there's more that you don't have access to.
> It's now sort of left the piece itself and it's become your own
> invention, so it's personal as well as being scary as well as being
> beautiful, which is what I really like about art like that.
> -----------------------------------------------------------------------------------------------------------
> Adventure Time http://theholenearthecenteroftheworld.com/



--
The idea is that a beautiful image is frameable. Everything you need
to see is there: It's everything you want, and it's very pleasing
because there's no extra information that you don't get to see.
Everything's in a nice package for you. But sublime art is
unframeable: It's an image or idea that implies that there's a bigger
image or idea that you can't see: You're only getting to look at a
fraction of it, and in that way it's both beautiful and scary, because
it's reminding you that there's more that you don't have access to.
It's now sort of left the piece itself and it's become your own
invention, so it's personal as well as being scary as well as being
beautiful, which is what I really like about art like that.
-----------------------------------------------------------------------------------------------------------
Adventure Time http://theholenearthecenteroftheworld.com/

--
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+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAGBeqiM07-VQWkDxHYmA_4H_Yaat6fWFKKwRJOS%2B6pVVjVy7KA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment