You have other problems before you solve this one.

your model<->option is a one-to-many but should be a many-to-many (many 
cars can have alloy-wheels and alloy-wheel is one of many possible options 
for each car).

Grid does now allow many-to-many very well but you can try:


db.define_table('make',
    Field('name'))

db.define_table('option',
    Field('name'))

db.define_table('model',
    Field('name'),
    Field('make_id', db.make),
    Field('options', 'list:reference option'))

Then I make a controller as follows:

def index(): 
    query = db.model.options.contains(db.option(name='Alloy Wheels').id)
    constraints = {'model':query}
    grid = SQLFORM.smartgrid(db.make,constraints=constraints)
    return dict(grid=grid)

On Tuesday, 2 October 2012 07:27:19 UTC-5, Dominic Cioccarelli wrote:
>
> I already posted this as a continuation of an existing question, but I 
> figured it may be better to break it out into a separate thread...
>
> What happens if I want the constraint to add to the existing constraints 
> that have been built by smartgrid? For example, if I have the model:
>
> db.define_table('make',
>     Field('name'))
>
> db.define_table('option',
>     Field('name'))
>
> m3t.define_table('model',
>     Field('name'),
>     Field('make_id', db.make),
>     Field('option_id', db.option))
>
> Then I make a controller as follows:
>
> def index():
>     grid = SQLFORM.smartgrid(db.make,constraints=constraints)
>     return dict(grid=grid)
>
> If I navigate to (for example)
>
> Alfa Romeo -> Gulia                      (Gulia being a type of Alfa)
>
> And I want to only display Gulias with alloy wheels, which constraint 
> should I define?
>
> If I define something like:
>
> query = option.name == 'Alloy Wheels'
> constraints = {'model':query}
>
> ... I'll get all the cars (irrespective of make and model) that have alloy 
> wheels. What I want is all the Alfa Romeo Gulias with Alloy Wheels. On the 
> other hand I remove the constraint and navigate from "Alfa Romeo" to 
> "Gulia" I'll get all Alfa Romo Gulias. What I need is a constraint that is 
> *added* to the generated constraints.
>
> Many thanks in advance,
> Dominic.
>
>

-- 



Reply via email to