I have the following models in Django that have a structure as follows:
class Office_Accounts(models.Model):
accountid = models.EmailField(max_length=200, unique=True)
validtill = models.DateField(default=datetime.now)
limit = models.CharField(max_length=2)
class Device(models.Model):
device_type = models.ForeignKey(DeviceType,to_field='device_type')
serial_number = models.CharField(max_length=200,unique=True)
in_use_by = models.ForeignKey(User,to_field='username')
brand = models.CharField(max_length=200,default="-", null=False)
model = models.CharField(max_length=200,default="-", null=False)
type_number = models.CharField(max_length=200,blank=True,null=True, default = None)
mac_address = models.CharField(max_length=200,blank=True,null=True, default = None)
invoice = models.FileField(upload_to='Device_Invoice', null=True, blank = True)
msofficeaccount = models.ForeignKey(Office_Accounts, to_field="accountid")
class Meta:
verbose_name_plural = "Devices"
def full_name(self):
return self.device_type + self.serial_number + self.brand
I will display both of the models in admin.py.
Now, I want to display the count of each accountid present in the field "msofficeaccount" (present in Device Models) in my admin page of Office_Accounts model. For an example if xyz#abc.com appears in 10 rows of msofficeaccount field then, the count should be displayed as 10 in Office_Accounts admin page. Can anyone please guide me how should I approach this problem to solve it?
You could add a method to your admin class that returns the count of related devices for each office_account, but that would be very inefficient. Instead you can override get_queryset to annotate the count from a database aggregation function:
from django.db.models import Count
class Office_AccountsAdmin(admin.ModelAdmin):
list_display = (..., 'device_count')
...
def get_queryset(self, request):
qs = super().get_queryset(request)
return qs.annotate(device_count=Count('device'))
(On a minor note, Python style is always to use CamelCase for class names, and Django style is to use singular model names, so your model should really be called OfficeAccount.)
Flask-Admin shows child objects defined by relationships in its standard edit view. For example, if User objects have Address children, looking at the edit view for User will show the Address child in the appropriate field. The user can then remove the object, or add another one.
I want users to be able to click through, or otherwise have the ability to enter the edit view of child objects. In the example I'm describing, the user should be able to access the edit view of the Address object directly from the edit view of the User object.
The only thing I've found at all related is inline_models, but this isn't a solution. The implementation is extremely fragile (it can't handle long distance relationships, for example). Flask-Admin is aware of child objects! I can see them in the view! I just want them to become a link to their own edit view...
Anyone have any idea how to accomplish this or can link to an example?
Here is a single file simple example of placing a link to another model's edit view in an edit view. It may help you or not.
I've used a User - Address relationship, a user has an address and address can have many users.
I've used Faker to generate sample data so you'll need to pip install faker into your environment.
The idea is to use Flask-Admin form rules and in this case I'm configuring form_edit_rules.
I've created two custom rules:
Link, inheriting BaseRule. The constructor takes three values; an endpoint, a name of an attribute to pass along with the endpoint in the Flask url_for method and finally the text to appear as the link. In this example the endpoint is 'address.edit_view' because this is the view we want to link to.
MultiLink, similar to Link accepts it works with a relation.
Here's the code (there's little error checking):
from random import randint
from flask import Flask, url_for
from flask_admin.contrib import sqla
from flask_admin import Admin
from flask_admin.form.rules import BaseRule
from faker import Faker
from flask_sqlalchemy import SQLAlchemy
from markupsafe import Markup
from sqlalchemy import func, select
from sqlalchemy.ext.hybrid import hybrid_property
fake = Faker()
# Create application
app = Flask(__name__)
# Create dummy secrey key so we can use sessions
app.config['SECRET_KEY'] = '123456790'
# Create in-memory database
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///:memory:'
# app.config['SQLALCHEMY_ECHO'] = True
db = SQLAlchemy(app)
# Flask views
#app.route('/')
def index():
return 'Click me to get to Admin!'
class Address(db.Model):
__tablename__ = 'addresses'
id = db.Column(db.Integer, primary_key=True)
number = db.Column(db.String(255))
street = db.Column(db.String(255))
city = db.Column(db.String(255))
country = db.Column(db.String(255))
#hybrid_property
def user_count(self):
return len(self.users)
#user_count.expression
def user_count(cls):
return select([func.count(User.id)]).where(User.address_id == cls.id).label("user_count")
def __unicode__(self):
return ', '.join(filter(None, [self.number, self.street, self.city, self.country]))
class User(db.Model):
__tablename__ = 'users'
id = db.Column(db.Integer, primary_key=True)
first_name = db.Column(db.String(255))
last_name = db.Column(db.String(255))
email = db.Column(db.String(254))
address_id = db.Column(db.Integer, db.ForeignKey('addresses.id'), index=True)
address = db.relationship(Address, backref=db.backref('users'))
def __str__(self):
return unicode(self).encode('utf-8')
def __unicode__(self):
return '{} {}'.format(self.first_name, self.last_name)
class Link(BaseRule):
def __init__(self, endpoint, attribute, text):
super(Link, self).__init__()
self.endpoint = endpoint
self.text = text
self.attribute = attribute
def __call__(self, form, form_opts=None, field_args=None):
if not field_args
field_args = {}
_id = getattr(form._obj, self.attribute, None)
if _id:
return Markup('{text}'.format(url=url_for(self.endpoint, id=_id), text=self.text))
class MultiLink(BaseRule):
def __init__(self, endpoint, relation, attribute):
super(MultiLink, self).__init__()
self.endpoint = endpoint
self.relation = relation
self.attribute = attribute
def __call__(self, form, form_opts=None, field_args=None):
if not field_args
field_args = {}
_hrefs = []
_objects = getattr(form._obj, self.relation)
for _obj in _objects:
_id = getattr(_obj, self.attribute, None)
_link = 'Edit {text}'.format(url=url_for(self.endpoint, id=_id), text=str(_obj))
_hrefs.append(_link)
return Markup('<br>'.join(_hrefs))
class UserAdmin(sqla.ModelView):
can_view_details = True
form_edit_rules = (
'first_name',
'last_name',
'email',
'address',
Link(endpoint='address.edit_view', attribute='address_id', text='Edit Address')
)
class AddressAdmin(sqla.ModelView):
can_view_details = True
column_list = ['number', 'street', 'city', 'country', 'user_count', 'users']
form_edit_rules = (
'number',
'street',
'city',
'country',
'users',
MultiLink(endpoint='user.edit_view', relation='users', attribute='id')
)
admin = Admin(app, template_mode="bootstrap3")
admin.add_view(UserAdmin(User, db.session))
admin.add_view(AddressAdmin(Address, db.session))
def build_db():
db.drop_all()
db.create_all()
for _ in range(0, 20):
_users = []
for _ in range(0, randint(1, 10)):
_user = User(
first_name=fake.first_name(),
last_name=fake.last_name(),
email=fake.safe_email(),
)
_users.append(_user)
_address = Address(
number=fake.random_digit_not_null(),
street=fake.secondary_address(),
city=fake.city(),
country=fake.country(),
users = _users
)
db.session.add(_address)
db.session.commit()
#app.before_first_request
def first_request():
build_db()
if __name__ == '__main__':
app.run(port=5000, debug=True)
I am trying to limit the fields to in my table. The only way I see to do it is through the PersonTable object with the field property like this fields = [first_name, last_name]. I want to do it from a request form. I tried to override the get_queryset() method but it did not work only passed in less data but the columns were still there just blank. Is there a good way to do it with the generic view?
class Person(models.Model):
first_name =models.CharField(max_length=200)
last_name =models.CharField(max_length=200)
user = models.ForeignKey("auth.User") dob = models.DateField()
class PersonTable(tables.Table):
class Meta:
model = Person
fields = [first_name, last_name]
class PersonList(SingleTableView):
model = Person
table_class = PersonTable
If anyone runs into this same issue, there is an exclude instance variable on the table class so you can just override get_table and do something like this in your view:
class PersonList(SingleTableView):
model = Person
table_class = PersonTable
template_name = "person.html"
def get_table(self):
table = super(PersonList, self).get_table()
columns = self.request.GET.getlist('column')
tuple_to_exclude = tuple(set(table.columns.names()) - set(columns))
table.exclude = tuple_to_exclude
return table
I am using MongoDB with Flask-MongoEngine as my ORM component to my web app.
I have structured the User document schema like so:
from ..core import db
class UserComics(db.EmbeddedDocument):
favorites = db.SortedListField(db.StringField(), default=None)
class UserSettings(db.EmbeddedDocument):
display_favs = db.BooleanField(default=False)
default_cal = db.StringField(default=None)
show_publishers = db.ListField(db.StringField(), default=None)
class UserTokens(db.EmbeddedDocument):
refresh_token = db.StringField(default=None)
access_token = db.StringField(default=None)
expire_time = db.StringField(default=None)
class User(db.Document, UserMixin):
# Save User document to this collection
meta = {'collection': 'users_test'}
userid = db.StringField()
full_name = db.StringField()
first_name = db.StringField()
last_name = db.StringField()
gender = db.StringField()
birthday = db.StringField()
email = db.EmailField()
friends = db.ListField(db.StringField())
date_creation = db.DateTimeField()
last_login = db.DateTimeField()
favorites = db.EmbeddedDocumentField(UserComics)
settings = db.EmbeddedDocumentField(UserSettings)
tokens = db.EmbeddedDocumentField(UserTokens)
However, When creating a new user like this (I have left out lines...):
def create_new_user(resp):
newUser = User()
....
newUser.settings.default_cal = resp['calendar']
....
newUser.save()
return
I run into this error:
AttributeError: 'NoneType' object has no attribute 'default_cal'
It seems to me that I am not using MongoEngines Embedded documents correctly but I do not know where I am going wrong.
Any help would be greatly appreciated!
Well you just have to create an embedded document object of the particular class, and then use it with the main document class, like so:
new_user = User()
user_settings = UserSettings()
user_settings.default_cal = resp['calendar']
new_user.settings = user_settings
# more stuff
new_user.save()
Note: Creating a new object only for the main document, does not automatically create the corresponding embedded document object(s), but while reading data ofcourse the case is different.
Edit:
As tbicr mentions below, we can also do this:
settings = db.EmbeddedDocumentField(UserSettings, default=UserSettings)
while declaring the field, that way we won't need to create the object as given in the first example.
I am developing my first Google App Engine project and I think I am misunderstanding something fundamental about the database and form models. I have the following python code:
class RegData(db.Model):
title = db.StringProperty()
forename = db.StringProperty()
surname = db.StringProperty()
interest = db.StringListProperty(choices=['TV','COMPUTING','SOCCER'])
class RegForm(djangoforms.ModelForm):
class Meta:
model = RegData
What I want to do is have the form render the 'interest' property as a set of check boxes instead of a text area. Is this possible?
Thanks.
from django import forms as form
you must add the above module in models.py
models.py
interest=(
('Tv', 'TV'),
('Computing', 'COMPUTING'),
('Soccer', 'SOCCER'),
)
class RegData(db.Model):
title = db.StringProperty()
forename = db.StringProperty()
surname = db.StringProperty()
interest = db.StringListProperty()
class RegForm(djangoforms.ModelForm):
interest= form.CheckboxSelectMultiple(choices=interest)
class Meta:
model = RegData
RegData is the table in your database (entity) , RegForm is that the user will see this form on your page.
main.py
class ShowForm(webapp.RequestHandler):
def get(self):
show(self)
def post(self):
show(self)
def show(self):
get = self.request.GET
post = self.request.POST
data = models.RegData()
if post:
form = models.RegForm(data=post, instance=RegDate)
if form.is_valid():
entity = form.save(commit=False)
entity.put()
else:
form = models.RegForm(instance=RegDate)