Django数据集转换:Package与Package_feature模型及数据处理咨询
Hey there! Let's break down how to perform various data transformation operations with your Django models. First, let's confirm the model setup and existing data we're working with.
Here's your model structure in Django:
from django.db import models from decimal import Decimal class Package(models.Model): name = models.CharField(max_length=200) class Package_feature(models.Model): category = models.CharField(max_length=200) price = models.DecimalField(max_digits=8, decimal_places=2, default=Decimal('0.00')) package = models.ForeignKey(Package, on_delete=models.CASCADE, null=True)
From your description, we have:
- Package table: Entries with
namevalues:set1,set2,set3,set4,set5 - Package_feature table:
- category: 'network equip ', price: 5000 → linked to Package
set1 - category: 'network equip ', price: 4000 → linked to Package
set2
(I’ll assume the rest follow a similar pattern for the examples below)
- category: 'network equip ', price: 5000 → linked to Package
Let's go through some useful transformations you might need:
1. Calculate total price per Package
If you want to sum up the cost of all features for each package, use Django's built-in aggregation tools:
from django.db.models import Sum package_totals = Package.objects.annotate( total_price=Sum('package_feature__price') ).values('name', 'total_price') # Output will be a queryset of dicts like: # [{'name': 'set1', 'total_price': Decimal('5000.00')}, ...]
2. Fetch packages with all their associated features
For a clean one-to-many lookup (getting each package alongside its features), use prefetching to optimize performance:
# Get packages with their features (prefetch_related avoids N+1 queries) packages_with_features = Package.objects.prefetch_related('package_feature_set').all() for package in packages_with_features: print(f"Package: {package.name}") for feature in package.package_feature_set.all(): print(f" Category: {feature.category}, Price: {feature.price}")
3. Filter features by category and link to their packages
If you only care about features in a specific category (like 'network equip ') and want to see which packages they belong to:
network_features = Package_feature.objects.filter( category='network equip ' ).select_related('package').values('package__name', 'category', 'price') # This gives you a queryset of network features paired with their linked package names
4. Compute average feature price per Package
To find the average price of features for each package, use the Avg aggregation:
from django.db.models import Avg package_avg_prices = Package.objects.annotate( avg_feature_price=Avg('package_feature__price') ).values('name', 'avg_feature_price')
5. Transform data into a lookup dictionary
If you need a quick way to access features by package name, convert the data into a Python dictionary:
package_feature_map = {} packages = Package.objects.prefetch_related('package_feature_set').all() for package in packages: features = [ {'category': f.category, 'price': f.price} for f in package.package_feature_set.all() ] package_feature_map[package.name] = features # Now you can do something like package_feature_map['set1'] to get all its features instantly
内容的提问来源于stack exchange,提问作者user3098791

