|
|
@ -148,7 +148,7 @@ class Product(models.Model): |
|
|
|
|
|
|
|
|
|
|
|
"""Product information""" |
|
|
|
"""Product information""" |
|
|
|
|
|
|
|
|
|
|
|
name = models.CharField(max_length=40) |
|
|
|
name = models.CharField('Name', max_length=40) |
|
|
|
</code></pre> |
|
|
|
</code></pre> |
|
|
|
|
|
|
|
|
|
|
|
</section> |
|
|
|
</section> |
|
|
@ -187,6 +187,9 @@ class ProductForm(ModelForm): |
|
|
|
class Meta: |
|
|
|
class Meta: |
|
|
|
model = Product |
|
|
|
model = Product |
|
|
|
</code></pre> |
|
|
|
</code></pre> |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<p>O "label" do campo ficará com o valor do primeiro parâmetro |
|
|
|
|
|
|
|
usado no field em ModelForms.</p> |
|
|
|
</section> |
|
|
|
</section> |
|
|
|
|
|
|
|
|
|
|
|
<section> |
|
|
|
<section> |
|
|
@ -323,6 +326,43 @@ urlpatterns = [ |
|
|
|
<img src="_images/goodgood-but-we-must-go-deeper.jpg" alt="Go deeper"/> |
|
|
|
<img src="_images/goodgood-but-we-must-go-deeper.jpg" alt="Go deeper"/> |
|
|
|
</section> |
|
|
|
</section> |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<section> |
|
|
|
|
|
|
|
<section> |
|
|
|
|
|
|
|
<h2>Índices</h2> |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<p>Cada campo pode ser indexado usando a propriedade |
|
|
|
|
|
|
|
<code>db_index</code>.</p> |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<pre><code class="hljs"> |
|
|
|
|
|
|
|
class Order(models.Model): |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
"""An order.""" |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
costumer.models.ForeignKey(User, db_index=True) |
|
|
|
|
|
|
|
</code></pre> |
|
|
|
|
|
|
|
</section> |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<section> |
|
|
|
|
|
|
|
<h2>Meta</h2> |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<p>Models podem ter uma classe de "Meta" com informações adicionais:</p> |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<pre><code class="hljs"> |
|
|
|
|
|
|
|
class Order(models.Model): |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
"""An order.""" |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
costumer = models.ForeignKey(User, db_index=True) |
|
|
|
|
|
|
|
date = models.DateTimeField(auto_now=True) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Meta: |
|
|
|
|
|
|
|
db_table = 'my_orders' |
|
|
|
|
|
|
|
index_together = [ |
|
|
|
|
|
|
|
('costumer', 'date') |
|
|
|
|
|
|
|
] |
|
|
|
|
|
|
|
</code></pre> |
|
|
|
|
|
|
|
</section> |
|
|
|
|
|
|
|
|
|
|
|
<section> |
|
|
|
<section> |
|
|
|
<h2>Relationships</h2> |
|
|
|
<h2>Relationships</h2> |
|
|
|
|
|
|
|
|
|
|
@ -345,6 +385,35 @@ class Order(models.Model): |
|
|
|
</code></pre> |
|
|
|
</code></pre> |
|
|
|
</section> |
|
|
|
</section> |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<section> |
|
|
|
|
|
|
|
<h2>"OneToMany"</h2> |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<p> |
|
|
|
|
|
|
|
Não existe relacionamento "one to many" (p.ex., um |
|
|
|
|
|
|
|
pedido tem vários ítens de pedido). Para isso, |
|
|
|
|
|
|
|
usa-se o <code>ForeignKey</code> na parte "many". |
|
|
|
|
|
|
|
</p> |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<pre><code class="hljs"> |
|
|
|
|
|
|
|
class Order(models.Model): |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
"""An order.""" |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
costumer = models.ForeignKey(Users) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class OrderItem(models.Model): |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
"""An item of an order.""" |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
order = models.ForeignKey(Order))metro |
|
|
|
|
|
|
|
product = models.ForeignKey(Product) |
|
|
|
|
|
|
|
quantity = models.PositiveIntegerField() |
|
|
|
|
|
|
|
</code></pre> |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
</section> |
|
|
|
|
|
|
|
</section> |
|
|
|
|
|
|
|
|
|
|
|
<section> |
|
|
|
<section> |
|
|
|
<h2>Inserts</h2> |
|
|
|
<h2>Inserts</h2> |
|
|
|
|
|
|
|
|
|
|
|