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

Edit functionality not working for Relationship Field (sqlmodel) #298

Closed
2 tasks done
tepelbaum opened this issue Sep 2, 2022 · 12 comments
Closed
2 tasks done

Edit functionality not working for Relationship Field (sqlmodel) #298

tepelbaum opened this issue Sep 2, 2022 · 12 comments

Comments

@tepelbaum
Copy link

Checklist

  • The bug is reproducible against the latest release or master.
  • There are no similar issues or pull requests to fix it yet.

Describe the bug

When we go on the sql edit admin interface, the Relationship field are not correctly displayed. see the sample code. Only one relation is created between foos and bars. But here is what is displayed

image

And when we click on edit

image

On the detailed view everything is fine though

image

Steps to reproduce the bug

Sample code

from sqlmodel import Field
from sqlmodel import Relationship
from sqlmodel import SQLModel
from typing import Optional, List
from app.db import create_db_and_tables
from sqlmodel import Session
from app.db import engine 
from sqladmin import ModelAdmin
from app.app import admin


class FooTable(SQLModel, table=True):
    """
    FooTable class, in one to many relationship with BarTable
    """
    __tablename__ = 'foo_table'
    id: Optional[int] = Field(default=None, primary_key=True)
    value: float
    bars: List['BarTable'] = Relationship(back_populates='foo')
        
class BarTable(SQLModel, table=True):
    """
    BarTable class, in many to one relationship with FooTable
    """
    __tablename__ = 'bar_table'
    id: Optional[int] = Field(default=None, primary_key=True)
    foo_id: Optional[int] = Field(default=None, foreign_key='foo_table.id')
    foo: Optional['FooTable'] = Relationship(back_populates='bars')
create_db_and_tables(BarTable)


with Session(engine) as session:
    for i in range(100):
        session.add(FooTable(value=i))
        session.add(BarTable(value=i))
    foo_100 = FooTable(value=100)
    session.add(foo_100)
    session.add(BarTable(value=i, foo=foo_100))
    session.commit()

    
class FooAdmin(ModelAdmin, model=FooTable):
    column_list = [FooTable.id, FooTable.value]
    
class BarAdmin(ModelAdmin, model=BarTable):
    column_list = [BarTable.id]
    

admin.add_view(FooAdmin)
admin.add_view(BarAdmin)

Expected behavior

Either the Relationship field should not be edited, or just the foreign key field should appear in the edit template.

Actual behavior

The relationship Fields cannot be edited AND the whole other table is displayed there

Debugging material

No response

Environment

image

Python 3.10 (python:3.10-slim base image). Latest sqladmin==0.3.0

Additional context

No response

@aminalaee
Copy link
Owner

Ah sorry I think I didn't get your question in our discussion, so you don't want to list all related objects in the edit/create page, right?
I'm already working on that:
#292
#193

Does it make sense?

@tepelbaum
Copy link
Author

Yep it does. Basically never show all possible other rows of another table in relationship (as the table could be 10M line long as in my case) :).

@aminalaee
Copy link
Owner

yeah, that's a very common use-case, in Django I think that's autocomplete_fields but in Flask-Admin that would be form_ajax_refs.

@tepelbaum
Copy link
Author

And when do you think it will have its equivalent in sqladmin? :)

@aminalaee
Copy link
Owner

The PR is almost ready, I just need to add the test coverage for it. I will let you know when it's merged and ideally get it tested by you before the new release :)

@tepelbaum
Copy link
Author

With pleasure!

@aminalaee
Copy link
Owner

@tepelbaum This is merged, it would be great if you can check out the main branch too.

@tepelbaum
Copy link
Author

Hi Amin! I'll do just that :)

@aminalaee
Copy link
Owner

Merged in #292 and will be available in 0.5.0.

@tepelbaum
Copy link
Author

Awesome! When is 0.5.0 planned?

@aminalaee
Copy link
Owner

Just released :)

@tepelbaum
Copy link
Author

Awesome! Thanks!!

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

2 participants