[GH-ISSUE #55] Documentation for defining foreign key strings in the admin site is soooo close... #35

Closed
opened 2026-02-26 01:32:57 +03:00 by kerem · 4 comments
Owner

Originally created by @abegong on GitHub (Dec 31, 2016).
Original GitHub issue: https://github.com/jeffknupp/sandman2/issues/55

In the admin interface, I need the foreign keys to show something more informative than <flask_sqlalchemy.users object at 0x7fdec43e6c90>.

Chapter 4 of the latest docs describes a fix for this problem. Unfortunately, the docs are incomplete.

I've tried to implement the fix, but haven't been able to get it to work. Editing models.py is not enough, and I'm not deeply familiar with the abstractions for SQLAlchemy and Flask-SQLAlchemy. Even after a couple hours of probing, I haven't been able to make sense of where to inject my edits.

  • Where should models.py be implemented? There's no real description of how to set up a sandman2 project when the CTL alone isn't enough.
  • Do I need to replace sandman2ctl with a tool that uses user_model when calling get_app?
  • Is there a good way to programmatically populate the unicode method across multiple classes? (e.g. use the value of the primary key as the default)

Thanks!

Originally created by @abegong on GitHub (Dec 31, 2016). Original GitHub issue: https://github.com/jeffknupp/sandman2/issues/55 In the admin interface, I need the foreign keys to show something more informative than `<flask_sqlalchemy.users object at 0x7fdec43e6c90>`. [Chapter 4 of the latest docs](https://media.readthedocs.org/pdf/sandman2/latest/sandman2.pdf) describes a fix for this problem. Unfortunately, the docs are incomplete. I've tried to implement the fix, but haven't been able to get it to work. Editing models.py is not enough, and I'm not deeply familiar with the abstractions for SQLAlchemy and Flask-SQLAlchemy. Even after a couple hours of probing, I haven't been able to make sense of where to inject my edits. * Where should models.py be implemented? There's no real description of how to set up a sandman2 project when the CTL alone isn't enough. * Do I need to replace sandman2ctl with a tool that uses user_model when calling get_app? * Is there a good way to programmatically populate the __unicode__ method across multiple classes? (e.g. use the value of the primary key as the default) Thanks!
kerem closed this issue 2026-02-26 01:32:57 +03:00
Author
Owner

@DanielJoyce commented on GitHub (Feb 15, 2017):

Still doesn't tell us where to put models.py, or how to get sandman2ctl to load and use it. This bug is still valid.

<!-- gh-comment-id:280137650 --> @DanielJoyce commented on GitHub (Feb 15, 2017): Still doesn't tell us where to put models.py, or how to get sandman2ctl to load and use it. This bug is still valid.
Author
Owner

@time-less-ness commented on GitHub (Mar 23, 2017):

I modified the models.py that was included with the pip package and put the classes at the bottom. But when I do, the top-level table disappears from the list of tables.

All the existing data looks nice, and the foreign key references do what they should, at the expense of no longer being able to query the table itself anymore.

I tried putting the Class extensions into /usr/local/bin/sandman2ctl as well, which worked precisely the same way.

<!-- gh-comment-id:288601175 --> @time-less-ness commented on GitHub (Mar 23, 2017): I modified the `models.py` that was included with the `pip` package and put the classes at the bottom. But when I do, the top-level table disappears from the list of tables. All the existing data looks nice, and the foreign key references do what they should, at the expense of no longer being able to query the table itself anymore. I tried putting the Class extensions into `/usr/local/bin/sandman2ctl` as well, which worked precisely the same way.
Author
Owner

@time-less-ness commented on GitHub (Mar 23, 2017):

It occurs to me that FK-referenced tables are meant to be static within the view of Sandman2's admin interface. So in order to fix my problem above, I run two instances of Sandman2, one with my FK-referenced tables without any Class extension, so that I can edit those tables, and another with them, so that I can edit the relationships sensibly.

It seems weird, but it does work.

<!-- gh-comment-id:288611159 --> @time-less-ness commented on GitHub (Mar 23, 2017): It occurs to me that FK-referenced tables are meant to be static within the view of Sandman2's admin interface. So in order to fix my problem above, I run two instances of Sandman2, one with my FK-referenced tables without any Class extension, so that I can edit those tables, and another with them, so that I can edit the relationships sensibly. It seems weird, but it does work.
Author
Owner

@PieterVeldman commented on GitHub (Jun 23, 2017):

Hi,

@abegong, I had a similar problem because I used to get "<sandman2.models.Employee object at ...>" in foreign keys in the admin view. Found out that I had to overwrite the str method instead of unicode method in models.py. I don't know why it is so, I just made a lucky guess that turned out to be correct.
As in your case, it didn't solve the issue right away. I had to modify run.py in order to list the tables.


run.py


app = get_app('sqlite+pysqlite:///tests/data/db.sqlite3', user_models=[ Artist, Album,Customer, Employee, Genre, Invoice, InvoiceLine, MediaType, Playlist, Track])
instead of
#app = get_app('sqlite+pysqlite:///tests/data/db.sqlite3')


In models.py , I addedd the str_ method.


models.py


...

class Genre(AutomapModel):

    """A music genre."""

    __tablename__ = 'Genre'

    def __unicode__(self):
        return self.Name

    __str__ = __unicode__

...


Maybe this will work for you, @philovivero.
Thanks @jeffknupp for this great tool.

<!-- gh-comment-id:310682224 --> @PieterVeldman commented on GitHub (Jun 23, 2017): Hi, @abegong, I had a similar problem because I used to get "<sandman2.models.Employee object at ...>" in foreign keys in the admin view. Found out that I had to overwrite the **str** method instead of **unicode** method in models.py. I don't know why it is so, I just made a lucky guess that turned out to be correct. As in your case, it didn't solve the issue right away. I had to modify run.py in order to list the tables. ________________________________________________________________________________ run.py ________________________________________________________________________________ `app = get_app('sqlite+pysqlite:///tests/data/db.sqlite3', user_models=[ Artist, Album,Customer, Employee, Genre, Invoice, InvoiceLine, MediaType, Playlist, Track]) ` instead of `#app = get_app('sqlite+pysqlite:///tests/data/db.sqlite3')` ________________________________________________________________________________ In models.py , I addedd the __str___ method. ________________________________________________________________________________ models.py ________________________________________________________________________________ ... ``` class Genre(AutomapModel): """A music genre.""" __tablename__ = 'Genre' def __unicode__(self): return self.Name __str__ = __unicode__ ``` ... ________________________________________________________________________________ Maybe this will work for you, @philovivero. Thanks @jeffknupp for this great tool.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/sandman2-jeffknupp#35
No description provided.