Default values for Item Fields()

694 views
Skip to first unread message

Subskii

unread,
Dec 13, 2011, 1:02:33 AM12/13/11
to scrapy-users
Hi all

I noticed in previous Scrapy documentations (http://doc.scrapy.org/en/
0.12/topics/items.html), you could specify a default value per Item
Field().

stock = Field(default=0)
...
product['stock'] # getting field with default value

This doesn't appear to work in later versions, and has been removed
from the documentation.
This will now only set the Item.fields attribute and cause an
exception on product['stock'] access.

What is the correct way do set a default Item Field() value now?

Thanks in advance.

Regards,

Adam M

Subskii

unread,
Dec 13, 2011, 2:35:29 AM12/13/11
to scrapy-users
Hi

I'm guessing I could override __init__() on my subclassed Item, and
then call self.setdefault() within the constructor to achieve the same
thing (Item's superclass is dict).

Whilst I'm new to Python- I do have experience in other languages.
Scrapy and Twisted are relatively new to me and so hearing any
recommendations are highly appreciated.


Regards,

Adam

Pablo Hoffman

unread,
Dec 16, 2011, 1:12:39 PM12/16/11
to scrapy...@googlegroups.com
That sounds like a proper way to implement defaults. The reason why
field defaults was removed from Scrapy is because there were many ways
to implement default field values depending on the project with slightly
different semantics (for example, should .iteritems() iterate over
populated or populated+default values?), so we decided to leave that
option to the users to implement them on their projects.
Reply all
Reply to author
Forward
0 new messages