Django practice (19): Customize the sequence-to-sequence relationship to implement Atom subscription

Source: Internet
Author: User

I remember someone told me that rails's has_sale: through is a "highlight". In Django's opinion, this function is not worth mentioning. In rails, you also need to manually create an association table (write the migration method). The "Syntax" of has_tables: through is just to customize the association relationship: multi-to-one association is implemented through a model class in the middle and to both ends.
In Django, the intermediate relational table of sequence-to-sequence is automatically created. If you want to specify your own Model class as the relational object, you only need to add a ManyToManyField attribute to the Model class to obtain the peer, and specify the though parameter. For example, we already have two orders-to-one relationships, LineItem ---> Product, LineItem --> Order. If you want to directly obtain the associated Order from the Product, you only need to add one row, the final Product is as follows:

class Product(models.Model):    title = models.CharField(max_length=100,unique=True)    description    = models.TextField()    image_url = models.URLField(max_length=200)    price = models.DecimalField(max_digits=8,decimal_places=2)    date_available = models.DateField()    orders = models.ManyToManyField(Order,through='LineItem')


Then, you can directly find the order containing the product through the product object:

$ python shell>>> from depot.depotapp.models import Product>>> p = Product(id=1)>>> p.orders>>> p.orders.all()[, ]


The purpose of this relationship is to generate a "subscription" for each product to check who has bought the product. We use Atom as the standard format. The generated Atom release format is as follows:

<? Xml version = "1.0" encoding = "UTF-8"?> <Feed xml: lang = "en-US" xmlns = ""> <id> tag: localhost, 2005: /products/3/who_bought </id> <link type = "text/html" href = "http: // localhost: 3000/depotapp "rel =" alternate "/> <link type =" application/atom + xml "href =" http: // localhost: 8000/depotapp/product/3/who_bought "rel =" self "/> <title> who purchased" the west of Cucumber's yellow watermelon "</title> <updated> 12:02:02 </updated> <entry> <id> tag: localhost, 2005: Orde R/1 </id> <published> 12:02:02 </published> <updated> 12:02:02 </updated> <link rel = "alternate" type = "text/html" href = "http: // localhost: 8000/orders/1 "/> <title> Order 1 </title> <summary type =" xhtml "> <div xmlns =" "> <p> I live in Beijing </p> </div> </summary> <author> <name> I am a buyer </name> <email> </email> </author> </entry> <entry> <id> tag: localhost, 2005: O Rder/3 </id> <published> 12:02:02 </published> <updated> 12:02:02 </updated> <link rel = "alternate" type = "text/html" href = "http: // localhost: 8000/orders/3 "/> <title> order 3 </title> <summary type =" xhtml "> <div xmlns =" "> <p> where do I live? </P> </div> </summary> <author> <name> I am a buyer 2 </name> <email> </email> </author> </ entry> </feed>


You may think that Atom is in xml format and can be implemented using Django REST framework, but this is not a good idea, because the xml generated by REST framework has its own format, the format is completely different from that of Atom. If you use the REST framework, you need to customize it, or even implement your own renderer (for example, AtomRenderer). This requires a deep understanding of the many details of the framework. For the sake of simplicity, we should consider using the Django template for implementation.

First, we design the url: http: // localhost: 8000/depotapp/product/[id]/who_bought. First, add urlpatterns in depot/depotapp/urls. py:

(r'product/(?P[^/]+)/who_bought$', atom_of_order)

Then, implement the view function in depot/depotapp/views. py:

def atom_of_order(request,id):    product = Product.objects.get(id = id)    t = get_template('depotapp/atom_order.xml')    c=RequestContext(request,locals())        return HttpResponse(t.render(c), mimetype='application/atom+xml')

Note that the mimetype is specified so that the browser knows that the returned xml is not html. The final task is to compile the template. Depot/templates/depotapp/atom_order.xml is as follows:

<? Xml version = "1.0" encoding = "UTF-8"?> <Feed xml: lang = "en-US" xmlns = ""> <id> tag: localhost, 2005:/product/{product. id}/who_bought </id> <link type = "text/html" href = "{% url depotapp. views. store_view %} "rel =" alternate "/> <link type =" application/atom + xml "href =" {% url depotapp. views. atom_of_order product. id %} "rel =" self "/> <title> who bought the {product. title }}" </title> <updated> 12:02:02 </updated> {% for order in product. orders. all %} <entry> <id> tag: localhost, 2005: order/{order. id }}</id> <published> 12:02:02 </published> <updated> 12:02:02 </updated> <link rel = "alternate" type = "text/html" href = "{% url depotapp. views. atom_of_order order. id %} "/> <title> order {order. id }}</title> <summary type = "xhtml"> <div xmlns = ""> <p >{{ order. address }}</p> </div> </summary> <author> <name >{{ order. name }}</name> <email >{{ order. email }}</email> </author> </entry >{% endfor %} </feed>


Using templates to implement Atom publishing is really simple, isn't it?


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.