Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

patch_data should work with empty arrays #43

Open
zcmander opened this issue May 10, 2016 · 3 comments
Open

patch_data should work with empty arrays #43

zcmander opened this issue May 10, 2016 · 3 comments

Comments

@zcmander
Copy link

zcmander commented May 10, 2016

Patch_data ignores empty arrays. This becomes issue when trying to remove all elements from specific model, like in the example, remove all items from order. I think patch_data should be in this situation {"items": []}, but it's actually just an empty dictionary.

$ python example.py 
wtforms version: 2.0.1
wtforms_json version: 0.2.10
form.data {'items': [{'name': 'Foo'}, {'name': 'Bar'}]}
form.patch_data {}
Traceback (most recent call last):
  File "example.py", line 67, in <module>
    assert(form.patch_data == {"items": []})
AssertionError

Here is a working example to reproduce the issue:

# -*- coding: utf-8 -*-
from sqlalchemy import Column, ForeignKey, Integer, Unicode
from sqlalchemy.orm import relationship
from sqlalchemy.ext.declarative import declarative_base
import wtforms
from wtforms import Form
from wtforms.fields import (
    FormField,
    StringField,
    FieldList,
)
import wtforms_json
wtforms_json.init()

Base = declarative_base()

import pprint
print "wtforms version:", wtforms.__version__
print "wtforms_json version:", wtforms_json.__version__


#1. Setup SQLAlchemy Models

class Item(Base):
    __tablename__ = 'item'
    id = Column(Integer, primary_key=True)
    order_id = Column(Integer, ForeignKey("order.id"))
    order = relationship("Order", backref="items")
    name = Column(Unicode())


class Order(Base):
    __tablename__ = 'order'
    id = Column(Integer, primary_key=True)


#2. Forms definition

class ItemForm(Form):
    name = StringField()


class OrderForm(Form):
    items = FieldList(FormField(ItemForm))


#3. Create existing instance and fill the form

instance = Order(items=[
    Item(name="Foo"),
    Item(name="Bar"),
])

json_data = {
    "items": []
}

form = OrderForm.from_json(json_data, obj=instance)

print u"form.data", pprint.pformat(form.data)
print u"form.patch_data", pprint.pformat(form.patch_data)


#4. patch_data should contain an empty list

assert(form.patch_data == {"items": []})

Is there anything that can be done to resolve this issue?

@gaetan-petit
Copy link

Same here, it is clearly a bug.

@zcmander
Copy link
Author

zcmander commented Jun 3, 2016

See PR: #45

@drimer
Copy link

drimer commented Sep 1, 2016

Is there any update on this? Or a work around to use for now? Tried specifying None, but that has the same result.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants