-
Notifications
You must be signed in to change notification settings - Fork 116
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
Dataframe and many-to-many relationship #40
Comments
I think I'm having the same problem. I have models like
Then, I try
which prints
and then I cannot/don't know how to access the inner object.
How to access Food name attribute? Thanks! |
Same for reverse foreign key relations. |
@evdoks sorry for the late response. You are correct!! I'll fix in in the next release. In the mean time here is a workaround. Using pandas and values_list QuerySets import pandas as pd
pd.DataFrame.from_records(list(Pizza.objects.all().values_list('name', 'toppings__name'))) |
@paulochf @bufke again sorry for the very tardy response. df = FoodCategory.objects.only("food__name").filter(category__showing=False).to_dataframe()
# Show the frame to see the column names
df.head()
#to get the food column you specify a list with the column names that you want ['food']
df[['food']]
#to get food and primary
df[['food, 'primary']] Note, you must define the @python_2_unicode_compatible
class Category(models.Model):
name = models.CharField(max_length=200)
showing = models.BooleanField(default=False)
objects = DataFrameManager()
def __str__(self):
return "{0}-{1}".format(
self.name,
self.showing
)
@python_2_unicode_compatible
class Food(models.Model):
name = models.CharField(max_length=200)
objects = DataFrameManager()
def __str__(self):
return "{}".format(self.name) Also you can get a DateFrame with exactly the columns you want by specifying them in a list as the first or FoodCategory.objects.filter(category__showing=False).to_dataframe(
['category__name', 'food__name', 'primary']
) |
Nice, @chrisdev! Thank you so much for explaining this! |
Is it possible to use django-pandas with models that stay in many-to-many relationship? It seems that currently this feature is not supported. E.g., for
df_pizza_with_toppings
does not contain any topping names.The text was updated successfully, but these errors were encountered: