Newbie : Object copy - weird error :-)

36 views
Skip to first unread message

mickae...@gmail.com

unread,
Jun 26, 2018, 12:17:32 PM6/26/18
to Django users
Hi all :-)

I'd like to archive some data.

I did that :

class AbstractDataModel(models.Model):

xxx


class Meta:
       
abstract = True


def __iter__(self):

       
return iter([self.xxx, self.yyy, self.zzz, self.aaa,
           
self.qqq, self.mode_bbb])


class DataModel(AbstractDataModel):


pass


class DataModelArchive(AbstractDataModel):
   
# https://stackoverflow.com/questions/21699707/python-how-to-copy-all-attibutes-from-base-class-to-derived-one
   
def __init__(self, source=None):
       
if source is not None:
           
self.__dict__.update(source.__dict__)



But when I want to access data in DataModelArchive, like DataModelArchive.objects.all() for example,
I get


Traceback (most recent call last):
 
File "/home/.../tests.py", line 1090, in test_archive
   
print(DataModelArchive.objects.all())
 
File "/home/.../venv/lib/python3.6/site-packages/django/db/models/query.py", line 248, in __repr__
    data
= list(self[:REPR_OUTPUT_SIZE + 1])
 
File "/home/.../venv/lib/python3.6/site-packages/django/db/models/query.py", line 272, in __iter__
   
self._fetch_all()
 
File "/home/.../venv/lib/python3.6/site-packages/django/db/models/query.py", line 1179, in _fetch_all
   
self._result_cache = list(self._iterable_class(self))
 
File "/home/.../venv/lib/python3.6/site-packages/django/db/models/query.py", line 63, in __iter__
    obj
= model_cls.from_db(db, init_list, row[model_fields_start:model_fields_end])
 
File "/home/.../lib/python3.6/site-packages/django/db/models/base.py", line 507, in from_db
   
new = cls(*values)
TypeError: __init__() takes from 1 to 2 positional arguments but 11 were given



What am I doing wrong ?

Thx
;-)

赖信桃

unread,
Jun 26, 2018, 10:35:18 PM6/26/18
to django...@googlegroups.com
Can you format your code or post it on a gist?
--
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...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/ac3a7a8f-f878-48b3-943a-eb47f6b03e7e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Julio Biason

unread,
Jun 27, 2018, 8:56:42 AM6/27/18
to django...@googlegroups.com
Hi Mikael,

The problem is happening because of this: __init__() takes from 1 to 2 positional arguments but 11 were given

On your new __init__(), you added just 2 parameters: `self` and `source`. But you overwrote the default `models.Model` init, which receives way more parameters (the original signature for this function is `__init__(self, *args, **kwargs)`).

What are you actually trying to achieve with this?

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



--
Julio Biason, Sofware Engineer
AZION  |  Deliver. Accelerate. Protect.
Office: +55 51 3083 8101  |  Mobile: +55 51 99907 0554

Mickael Barbo

unread,
Jun 29, 2018, 3:43:20 AM6/29/18
to django...@googlegroups.com
Hi 赖信桃,

Sorry for formating problem. Here it is


class AbstractDataModel(models.Model):

    xxx


    class Meta:
       
abstract = True


    def __iter__(self):

        
return iter([self.xxx, self.yyy, self.zzz, self.aaa,
             
self.qqq, self.mode_bbb])


class DataModel(AbstractDataModel):

    pass


class DataModelArchive(AbstractDataModel):
   
# https://stackoverflow.com/questions/21699707/python-how-to-copy-all-attibutes-from-base-class-to-derived-one
   
def __init__(self, source=None):
       
if source is not None:
           
self.__dict__.update(source.__dict__)


To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.

--
You received this message because you are subscribed to a topic in the Google Groups "Django users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-users/gHKY0BAcTi4/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-users+unsubscribe@googlegroups.com.

To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.

Mickael Barbo

unread,
Jun 29, 2018, 3:45:19 AM6/29/18
to django...@googlegroups.com
Hello  Julio,

Ok, I see.

I try to create a way to archive (copy) the same object instance on an other DataBase.

I followed this advice : # https://stackoverflow.com/questions/21699707/python-how-to-copy-all-attibutes-from-base-class-to-derived-one


--
You received this message because you are subscribed to a topic in the Google Groups "Django users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-users/gHKY0BAcTi4/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-users+unsubscribe@googlegroups.com.

To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.

Melvyn Sopacua

unread,
Jul 2, 2018, 6:34:14 PM7/2/18
to django...@googlegroups.com

On vrijdag 29 juni 2018 09:44:40 CEST Mickael Barbo wrote:

 

> I try to create a way to archive (copy) the same object instance on an

> other DataBase.

>

> I followed this advice : # https://stackoverflow.com/

> questions/21699707/python-how-to-copy-all-attibutes-from-

> base-class-to-derived-one

 

That advice is for normal python classes. Django models have some restrictions, one being that fields are special attributes. Messing with a model's __init__() is not something you normally do as a model's class creation is highly customized.

 

If you're really using 2 different databases, a model's save() operation supports a `using` keyword that allows you to select the database connection. Archiving becomes really easy that way.

 

 

--

Melvyn Sopacua

mickae...@gmail.com

unread,
Jul 13, 2018, 7:15:07 AM7/13/18
to Django users
Thanks you Melvyn, this is it :-)

Have a nice day.

Regards
Reply all
Reply to author
Forward
0 new messages