Giter Site home page Giter Site logo

priestd09 / django-generic-m2m Goto Github PK

View Code? Open in Web Editor NEW

This project forked from coleifer/django-generic-m2m

0.0 1.0 0.0 428 KB

relate anything to anything

Home Page: http://readthedocs.org/docs/django-generic-m2m/en/latest/

License: MIT License

Python 72.77% CSS 13.50% JavaScript 4.02% HTML 9.71%

django-generic-m2m's Introduction

django-generic-m2m

relate anything to anything. the image below is a screenshot of the example app and shows a blog post that has been "related" to 2 "Place" models and a "City" model:

image

check the documentation for more examples and an in-depth description of the app (or keep reading for the 30 second version).

what it does

the purpose of this project is to allow you to create database-level relationships between various objects using a consistent api.

example app

bundled with the source code is an example app which shows how generic-m2m can be used to create "tags" between models. it uses nathanborror's basic apps with django-completion (shameless plug) to allow users to "autocomplete" various relationships between models, so if I'm a user and want to create a new blog post I can tag it with relationships to objects representing a city, a place, a funny photo of a cat, etc.

image

quick overview

say you have a couple models:

class Food(models.Model):
    name = models.CharField(max_length=255)

    related = RelatedObjectsDescriptor()

    def __unicode__(self):
        return self.name


class Beverage(models.Model):
    name = models.CharField(max_length=255)

    related = RelatedObjectsDescriptor()

    def __unicode__(self):
        return self.name

Here's a sample interactive interpreter session to show the basic API:

>>> pizza = Food.objects.create(name='pizza')
>>> pepperoni = Food.objects.create(name='pepperoni')
>>> beer = Beverage.objects.create(name='beer')
>>> soda = Beverage.objects.create(name='soda')

>>> pizza.related.connect(pepperoni)
<RelatedObject: pizza related to pepperoni ("")>

>>> pizza.related.connect(beer)
<RelatedObject: pizza related to beer ("")>

>>> pepperoni.related.related_to()  
[<RelatedObject: pizza related to pepperoni ("")>]

>>> pizza.related.all()
[<RelatedObject: pizza related to beer ("")>, <RelatedObject: pizza related to pepperoni ("")>]

>>> pizza.related.all().generic_objects()
[<Beverage: beer>, <Food: pepperoni>]

>>> Food.related.all()
[<RelatedObject: pizza related to beer ("")>, <RelatedObject: pizza related to pepperoni ("")>]

django-generic-m2m's People

Contributors

coleifer avatar coordt avatar jayfk avatar marky1991 avatar n3storm avatar quiqueporta avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.