Re: Re: prob. with non-related objects deleted

2015-01-06 Thread James Schneider
No problem. If you are happy with the functionality, then keep it simple
and go with what you know.

However, I would highly, highly recommend that you do not rely on system
level packages for application development (such as those installed as .deb
packages). I would move your entire project into a Python virtual
environment and manage all package versions via pip. It is definitely worth
a Google...

If you have any plans to make it a long-standing project, future developers
who look at your code will thank you.

-James

On Tue, Jan 6, 2015 at 1:08 PM, <sneak...@gmx.net> wrote:

>  thank you for your answer, james.
>
> i had a quick look, but unfortunatly, both are not readily available as
> .deb packages for python3, so i just just had some tries and came away with
> the following solution. in the previously mentioned "removeEntry" method, i
> changed the "with" block to the following:
>
>  with transaction.atomic():
> if self.prev_entry == self.next_entry:
> self.prev_entry.next_entry = self.prev_entry
> self.prev_entry.prev_entry = self.prev_entry
> self.prev_entry.save()
> else:
> self.prev_entry.next_entry = self.next_entry
> self.prev_entry.save()
> self.next_entry.prev_entry = self.prev_entry
> self.next_entry.save()
> self.next_entry = None
> self.prev_entry = None
> self.save()
> self.delete()
>
>  now it works as it should. i have a feeling that in the previous
> version, when there where only 2 entries left, self.prev_entry.next_entry
> and self.next_entry.prev_entry were different objects in django, although
> they are the same database objects.
>
> best regards
>
>
> tom
>
>
> *Gesendet:* Dienstag, 06. Januar 2015 um 12:56 Uhr
> *Von:* "James Schneider" <jrschneide...@gmail.com>
> *An:* django-users@googlegroups.com
> *Betreff:* Re: prob. with non-related objects deleted
>
> This looks a lot like a basic MPTT tree. Have you looked at django-mptt or
> django-treebeard? Either of those packages could probably save you a ton of
> headache here, assuming that your lists contain all of the same type of
> model, which it appears they do.
>
>
> http://django-mptt.github.io/django-mptt/overview.html#what-is-modified-preorder-tree-traversal
>
> https://tabo.pe/projects/django-treebeard/docs/tip/
>
> -James
> On Jan 6, 2015 3:30 AM, <sneak...@gmx.net> wrote:
>>
>> hello all,
>>
>> my problem is the following (the models.py is attached):
>> i want to implement a double-linked list (type BasicEntry) with the first
>> entry having an empty name. each entry has two attributes prev_entry and
>> next_entry to its neigbours. the first entry is created automaticaly and
>> should never be removed. upon removing the second to last entry (using the
>> function removeEntry), the last entry is also removed, although i do not
>> know why. i tracked it down to the "collect" function in "deletion.py" on
>> line 190:
>>
>> for related in model._meta.get_all_related_objects(
>> include_hidden=True, include_proxy_eq=True):
>>
>> do i misunderstand django or is this a known problem or "feature"?
>> although i think i removed all references from the to be removed entry to
>> the other entries...
>>
>>
>> class BasicEntry(models.Model):
>> name = models.CharField(max_length=10)
>> entry_col = models.ForeignKey(EntryCollection)
>> next_entry = models.ForeignKey('BasicEntry', related_name='next',
>> null=True, default=None)
>> prev_entry = models.ForeignKey('BasicEntry', related_name='prev',
>> null=True, default=None)
>>
>> def addEntry(self, name):
>> try_nr = 1
>> new_entry = None
>> while True:
>> try:
>> with transaction.atomic():
>> new_entry = BasicEntry(name=name,
>>entry_col=self.entry_col,
>>next_entry=self.next_entry,
>>prev_entry=self)
>> new_entry.save()
>> self.next_entry.prev_entry = new_entry
>> self.next_entry.save()
>> self.next_entry = new_entry
>> if self.prev_entry == self:
&g

Aw: Re: prob. with non-related objects deleted

2015-01-06 Thread sneaky56


thank you for your answer, james.

 

i had a quick look, but unfortunatly, both are not readily available as .deb packages for python3, so i just just had some tries and came away with the following solution. in the previously mentioned "removeEntry" method, i changed the "with" block to the following:

 


    with transaction.atomic():
    if self.prev_entry == self.next_entry:
    self.prev_entry.next_entry = self.prev_entry
    self.prev_entry.prev_entry = self.prev_entry
    self.prev_entry.save()
    else:
    self.prev_entry.next_entry = self.next_entry
    self.prev_entry.save()
    self.next_entry.prev_entry = self.prev_entry
    self.next_entry.save()
    self.next_entry = None
    self.prev_entry = None
    self.save()
    self.delete()

 


now it works as it should. i have a feeling that in the previous version, when there where only 2 entries left, self.prev_entry.next_entry and self.next_entry.prev_entry were different objects in django, although they are the same database objects.

 

best regards

 

 

tom

 

 

Gesendet: Dienstag, 06. Januar 2015 um 12:56 Uhr
Von: "James Schneider" <jrschneide...@gmail.com>
An: django-users@googlegroups.com
Betreff: Re: prob. with non-related objects deleted


This looks a lot like a basic MPTT tree. Have you looked at django-mptt or django-treebeard? Either of those packages could probably save you a ton of headache here, assuming that your lists contain all of the same type of model, which it appears they do.

http://django-mptt.github.io/django-mptt/overview.html#what-is-modified-preorder-tree-traversal

https://tabo.pe/projects/django-treebeard/docs/tip/

-James

On Jan 6, 2015 3:30 AM, <sneak...@gmx.net> wrote:

hello all,

my problem is the following (the models.py is attached):
i want to implement a double-linked list (type BasicEntry) with the first entry having an empty name. each entry has two attributes prev_entry and next_entry to its neigbours. the first entry is created automaticaly and should never be removed. upon removing the second to last entry (using the function removeEntry), the last entry is also removed, although i do not know why. i tracked it down to the "collect" function in "deletion.py" on line 190:

    for related in model._meta.get_all_related_objects(
                    include_hidden=True, include_proxy_eq=True):

do i misunderstand django or is this a known problem or "feature"? although i think i removed all references from the to be removed entry to the other entries...


class BasicEntry(models.Model):
    name = models.CharField(max_length=10)
    entry_col = models.ForeignKey(EntryCollection)
    next_entry = models.ForeignKey('BasicEntry', related_name='next', null=True, default=None)
    prev_entry = models.ForeignKey('BasicEntry', related_name='prev', null=True, default=None)

    def addEntry(self, name):
        try_nr = 1
        new_entry = None
        while True:
            try:
                with transaction.atomic():
                    new_entry = BasicEntry(name=name,
                                           entry_col=self.entry_col,
                                           next_entry=self.next_entry,
                                           prev_entry=self)
                    new_entry.save()
                    self.next_entry.prev_entry = new_entry
                    self.next_entry.save()
                    self.next_entry = new_entry
                    if self.prev_entry == self:
                        self.prev_entry = new_entry
                    self.save()
            except IntegrityError as e:
                print('addEntry: try %d' %(try_nr))
                try_nr += 1
                if try_nr >= DB_Transaction_Tries:
                    raise(e)
                time.sleep(DB_Transaction_sleep)
            else:
                break
        return(new_entry)

    def removeEntry(self):
        if self.name == '':
            print('** The "virtual" first track can not be removed!')
            return
        try_nr = 1
        while True:
            try:
                with transaction.atomic():
                    self.prev_entry.next_entry = self.next_entry
                    self.prev_entry.save()
                    self.next_entry.prev_entry = self.prev_entry
                    self.next_entry.save()
                    self.next_entry = None
                    self.prev_entry = None
                    self.save()
                    self.delete()
            except IntegrityError as e:
                print('removeEntry: try %d' %(try_nr))
                try_nr += 1
                if try_nr >= DB_Transaction_Tries:
      

Re: prob. with non-related objects deleted

2015-01-06 Thread James Schneider
This looks a lot like a basic MPTT tree. Have you looked at django-mptt or
django-treebeard? Either of those packages could probably save you a ton of
headache here, assuming that your lists contain all of the same type of
model, which it appears they do.

http://django-mptt.github.io/django-mptt/overview.html#what-is-modified-preorder-tree-traversal

https://tabo.pe/projects/django-treebeard/docs/tip/

-James
 On Jan 6, 2015 3:30 AM,  wrote:

> hello all,
>
> my problem is the following (the models.py is attached):
> i want to implement a double-linked list (type BasicEntry) with the first
> entry having an empty name. each entry has two attributes prev_entry and
> next_entry to its neigbours. the first entry is created automaticaly and
> should never be removed. upon removing the second to last entry (using the
> function removeEntry), the last entry is also removed, although i do not
> know why. i tracked it down to the "collect" function in "deletion.py" on
> line 190:
>
> for related in model._meta.get_all_related_objects(
> include_hidden=True, include_proxy_eq=True):
>
> do i misunderstand django or is this a known problem or "feature"?
> although i think i removed all references from the to be removed entry to
> the other entries...
>
>
> class BasicEntry(models.Model):
> name = models.CharField(max_length=10)
> entry_col = models.ForeignKey(EntryCollection)
> next_entry = models.ForeignKey('BasicEntry', related_name='next',
> null=True, default=None)
> prev_entry = models.ForeignKey('BasicEntry', related_name='prev',
> null=True, default=None)
>
> def addEntry(self, name):
> try_nr = 1
> new_entry = None
> while True:
> try:
> with transaction.atomic():
> new_entry = BasicEntry(name=name,
>entry_col=self.entry_col,
>next_entry=self.next_entry,
>prev_entry=self)
> new_entry.save()
> self.next_entry.prev_entry = new_entry
> self.next_entry.save()
> self.next_entry = new_entry
> if self.prev_entry == self:
> self.prev_entry = new_entry
> self.save()
> except IntegrityError as e:
> print('addEntry: try %d' %(try_nr))
> try_nr += 1
> if try_nr >= DB_Transaction_Tries:
> raise(e)
> time.sleep(DB_Transaction_sleep)
> else:
> break
> return(new_entry)
>
> def removeEntry(self):
> if self.name == '':
> print('** The "virtual" first track can not be removed!')
> return
> try_nr = 1
> while True:
> try:
> with transaction.atomic():
> self.prev_entry.next_entry = self.next_entry
> self.prev_entry.save()
> self.next_entry.prev_entry = self.prev_entry
> self.next_entry.save()
> self.next_entry = None
> self.prev_entry = None
> self.save()
> self.delete()
> except IntegrityError as e:
> print('removeEntry: try %d' %(try_nr))
> try_nr += 1
> if try_nr >= DB_Transaction_Tries:
> raise(e)
> time.sleep(DB_Transaction_sleep)
> else:
> break
>
>
>
> a few details:
> linux (debian amd64)
> python3 (v3.4.2-2)
> django (v1.7.1-1)
>
> here's an example in postgres:
>
> django=> select * from entry_test_basicentry order by id;
> id | name | entry_col_id | next_entry_id | prev_entry_id
> +---+--+---+---
> 33 | | 14 | 34 | 35
> 34 | test1 | 14 | 35 | 33
> 35 | test2 | 14 | 33 | 34
> (3 rows)
>
>
> after removing ID 34:
>
> django=> select * from entry_test_basicentry order by id;
> id | name | entry_col_id | next_entry_id | prev_entry_id
> +---+--+---+---
> 33 | | 14 | 35 | 35
> 35 | test2 | 14 | 33 | 33
> (2 rows)
>
>
> after removing ID 35:
>
> django=> select * from entry_test_basicentry order by id;
> id | name | entry_col_id | next_entry_id | prev_entry_id
> +--+--+---+---
> (0 rows)
>
>
> thanks in advance and best regards
>
>
> tom
>
> --
> 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+unsubscr...@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.