构建一个小型反应应用程序,通过地理定位(由浏览器确定为子组件作为道具).
第一个组件:App.jsx
import React, {Component} from 'react';
import DateTime from './components/dateTime/_dateTime.jsx';
import Weather from './components/weather/_weather.jsx';
import Welcome from './components/welcome/_welcome.jsx';
require ('../sass/index.scss');
export default class App extends Component {
constructor() {
super();
this.state = {
latitude: '',
longitude: ''
};
this.showPosition = this.showPosition.bind(this);
}
startApp () {
this.getLocation();
}
getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(this.showPosition);
} else {
console.log("Geolocation is not supported by this browser.");
}
}
showPosition(position) {
this.setState({
latitude: position.coords.latitude,
longitude: position.coords.longitude
})
}
componentWillMount () {
this.startApp();
} …Run Code Online (Sandbox Code Playgroud) 我有类Invoice,其中(简化)具有以下属性:
class Invoice(models.Model)
number = models.CharField(verbose_name="Number", max_length=16)
issue_date = models.DateTimeField(verbose_name="Issue date", default=datetime.now)
total = models.FloatField(verbose_name="Total", blank=True, null=True)
Run Code Online (Sandbox Code Playgroud)
然后,我有InvoiceLine类,它代表发票可以拥有的行/行:
class InvoiceLine(models.Model):
invoice = models.ForeignKey(Invoice, verbose_name="Invoice")
description = models.CharField(verbose_name="Description", max_length=64)
line_total = models.FloatField(verbose_name="Line total")
Run Code Online (Sandbox Code Playgroud)
InvoiceLine是Invoice的内联,我想要实现的是,当在管理员中有人用发票线(一个或多个)保存发票时,计算发票总额.我试图通过覆盖方法保存来做到这一点:
class Invoice(models.Model)
number = models.CharField(verbose_name="Number", max_length=16)
issue_date = models.DateTimeField(verbose_name="Issue date", default=datetime.now)
total = models.FloatField(verbose_name="Total", blank=True, null=True)
def save(self, *args, **kwargs):
invoice_lines = InvoiceLine.objects.filter(invoice=self.id)
self.total = 0
for line in invoice_lines:
self.total=self.total+line.line_total
super(Invoice, self).save(*args, **kwargs)
Run Code Online (Sandbox Code Playgroud)
问题是,当我在InvoiceLine中添加元素时,第一次保存并调用functionsave时,内联(InvoiceLine)中的新元素尚未存储,所以当我这样做时InvoiceLine.objects.filter(invoice=self.id),它们不会被考虑在内.因此,这种方法的唯一方法是节省两次.我也尝试过:
def save(self, *args, **kwargs):
super(Invoice, self).save(*args, **kwargs)
invoice_lines = InvoiceLine.objects.filter(invoice=self.pk) …Run Code Online (Sandbox Code Playgroud) django django-models django-admin django-modeladmin django-admin-actions