def runTest(self): vwgruppe = Manufacturer(name="VWGruppe") vwgruppe.save() vw = Brand(name="VW") vw.save() vw.manufacturer = vwgruppe golf = Car(name="Golf") golf.save() golf.brand = vw bulli = Car(name="Bulli") bulli.save() bulli.brand = vw kaefer = Car(name=u"Käfer") kaefer.save() kaefer.brand = vw # Removing only works ReverseForeignKeyManagers. Instances-to-be- # removed will be removed from the relationship. vw.car_set.remove([golf]) self.assert_(golf not in vw.car_set) self.assert_(golf.brand is None) self.assert_(list(vw.car_set) == [bulli, kaefer])
def runTest(self): vwgruppe = Manufacturer(name="VWGruppe") vwgruppe.save() vw = Brand(name="VW") vw.save() vw.manufacturer = vwgruppe golf = Car(name="Golf") golf.save() golf.brand = vw bulli = Car(name="Bulli") bulli.save() bulli.brand = vw kaefer = Car(name=u"Käfer") kaefer.save() kaefer.brand = vw # db setup complete # delete del golf.brand self.assert_(golf.brand is None) self.assert_(golf not in vw.car_set) del vw.car_set self.assert_(bulli.brand is None) self.assert_(kaefer.brand is None) self.assert_(bulli not in vw.car_set) self.assert_(kaefer not in vw.car_set)
def runTest(self): vwgruppe = Manufacturer(name="VWGruppe") vwgruppe.save() vw = Brand(name="VW") vw.save() vw.manufacturer = vwgruppe golf = Car(name="Golf") golf.save() bulli = Car(name="Bulli") bulli.save() kaefer = Car(name=u"Käfer") kaefer.save() # db setup complete # Adding on foreign key relationships only works in the reverse # direction: # Car.brand only ever holds one Brand instance, only setting is # supported. # But Brand.car_set can hold multiple Cars, so adding works here. vw.car_set.add([golf, kaefer, bulli]) self.assert_(golf in vw.car_set) self.assert_(kaefer in vw.car_set) self.assert_(bulli in vw.car_set) self.assert_(golf.brand is vw) self.assert_(bulli.brand is vw) self.assert_(kaefer.brand is vw)
def runTest(self): vwgruppe = Manufacturer(name="VWGruppe") vwgruppe.save() vw = Brand(name="VW") vw.save() vw.manufacturer = vwgruppe golf = Car(name="Golf") golf.save() golf.brand = vw bulli = Car(name="Bulli") bulli.save() bulli.brand = vw kaefer = Car(name=u"Käfer") kaefer.save() kaefer.brand = vw
def runTest(self): vwgruppe = Manufacturer(name="VWGruppe") vwgruppe.save() vw = Brand(name="VW") vw.save() vw.manufacturer = vwgruppe golf = Car(name="Golf") golf.save() golf.brand = vw bulli = Car(name="Bulli") bulli.save() bulli.brand = vw kaefer = Car(name=u"Käfer") kaefer.save() kaefer.brand = vw # db setup complete self.assert_(vw is Brand.objects.get(name="VW")) self.assert_(kaefer.brand is vw) self.assert_(kaefer.brand.car_set[-1] is kaefer) self.assert_(kaefer.brand.car_set[-1].brand is vw) self.assert_(kaefer.brand.manufacturer is vwgruppe) self.assert_(kaefer.brand.car_set[0].brand.manufacturer is vwgruppe) self.assert_(vwgruppe.brand_set[0] is vw) self.assert_(vwgruppe.brand_set[0].car_set[1] is bulli) self.assert_(vwgruppe.brand_set[0].car_set[1].brand.manufacturer is vwgruppe)
def runTest(self): bmwgroup = Manufacturer(name="Bayerische Motoren Werke AG") bmwgroup.save() bmw = Brand(name="BMW") bmw.save() bmw.manufacturer = bmwgroup bmw3er = Car(name="3er") bmw7er = Car(name="7er") bmwX3 = Car(name="X3") bmw3er.save() bmw7er.save() bmwX3.save() bmw3er.brand = bmw bmw7er.brand = bmw bmwX3.brand = bmw daimlergroup = Manufacturer(name="Daimler AG") daimlergroup.save() benz = Brand(name="Mercedes Benz") benz.save() benz.manufacturer = daimlergroup benzT = Car(name="T Modell") benzGLK = Car(name="GLK") benzSLK = Car(name="SLK") benzT.save() benzGLK.save() benzSLK.save() benzT.brand = benz benzGLK.brand = benz benzSLK.brand = benz harry = Driver(first_name="Harry", last_name="Klein") ayrton = Driver(first_name="Ayrton", last_name="Senna") harry.save() ayrton.save() harry.cars = [benzT, bmw3er, bmwX3] ayrton.cars.add([benzGLK, benzSLK, bmw7er, bmwX3]) benzTkey = Key(serial=12) benzTkey.save() bmwX3key = Key(serial=10039) bmwX3key.save() benzTkey.owner = harry bmwX3key.owner = ayrton #db setup complete # all returns all instances of a Model allcars = list(Car.objects.all()) self.assert_(allcars == [bmw3er, bmw7er, bmwX3, benzT, benzGLK, benzSLK]) alldrivers = list(Driver.objects.all()) self.assert_(alldrivers == [harry, ayrton])
def runTest(self): bmwgroup = Manufacturer(name="Bayerische Motoren Werke AG") bmwgroup.save() bmw = Brand(name="BMW") bmw.save() bmw.manufacturer = bmwgroup bmw3er = Car(name="3er") bmw7er = Car(name="7er") bmwX3 = Car(name="X3") bmw3er.save() bmw7er.save() bmwX3.save() bmw3er.brand = bmw bmw7er.brand = bmw bmwX3.brand = bmw daimlergroup = Manufacturer(name="Daimler AG") daimlergroup.save() benz = Brand(name="Mercedes Benz") benz.save() benz.manufacturer = daimlergroup benzT = Car(name="T Modell") benzGLK = Car(name="GLK") benzSLK = Car(name="SLK") benzT.save() benzGLK.save() benzSLK.save() benzT.brand = benz benzGLK.brand = benz benzSLK.brand = benz harry = Driver(first_name="Harry", last_name="Klein") ayrton = Driver(first_name="Ayrton", last_name="Senna") harry.save() ayrton.save() harry.cars = [benzT, bmw3er, bmwX3] ayrton.cars.add([benzGLK, benzSLK, bmw7er, bmwX3]) benzTkey = Key(serial=12) benzTkey.save() bmwX3key = Key(serial=10039) bmwX3key.save() benzTkey.owner = harry bmwX3key.owner = ayrton keys = [] for x in range(10): k = Key(serial=x) k.save() keys.append(k) drivernames = ( ("Mina", "da Silva Sanches"), ("Gundula", "Gause"), ("Sandra", "Maischberger"), ("Bruno", "Gaspard"), ("Hermi", "Tock"), ("Willi", "Gunther"), ("Gisbert", "Geier") ) drivers = [] for f, l in drivernames: d = Driver(first_name=f, last_name=l) d.save() drivers.append(d) #db setup complete # make a search for certain attributes of a Model # startswith qs = Driver.objects.filter(last_name__startswith="G") self.assert_(list(qs) == [drivers[1], drivers[3], drivers[5], drivers[6]]) # endswith qs = Car.objects.filter(name__endswith="er") self.assert_(list(qs) == [bmw3er, bmw7er]) self.assert_(list(qs)[0].name == "3er") self.assert_(list(qs)[1].name == "7er") # contains qs = Driver.objects.filter(last_name__contains="aspa") self.assert_(list(qs) == [drivers[3]] and list(qs)[0].first_name == "Bruno") # gt qs = Key.objects.filter(serial__gt=8) self.assert_(list(qs) == [benzTkey, bmwX3key, keys[9]]) # gte qs = Key.objects.filter(serial__gte=8) self.assert_(list(qs) == [benzTkey, bmwX3key, keys[8], keys[9]]) # lt qs = Key.objects.filter(serial__lt=3) self.assert_(list(qs) == [keys[0], keys[1], keys[2]]) # lte qs = Key.objects.filter(serial__lte=3) self.assert_(list(qs) == [keys[0], keys[1], keys[2], keys[3]]) # exact qs = Car.objects.filter(name__exact="3er") self.assert_(list(qs) == [bmw3er]) # in qs = Key.objects.filter(serial__in=(1,3,5)) self.assert_(list(qs) == [keys[1], keys[3], keys[5]]) # between qs = Key.objects.filter(serial__between=(3, 1)) self.assert_(list(qs) == [keys[2]]) qs = Key.objects.filter(serial__between=(3, 5)) self.assert_(list(qs) == [keys[4]])
def runTest(self): bmwgroup = Manufacturer(name="Bayerische Motoren Werke AG") bmwgroup.save() bmw = Brand(name="BMW") bmw.save() bmw.manufacturer = bmwgroup bmw3er = Car(name="3er") bmw7er = Car(name="7er") bmwX3 = Car(name="X3") bmw3er.save() bmw7er.save() bmwX3.save() bmw3er.brand = bmw bmw7er.brand = bmw bmwX3.brand = bmw daimlergroup = Manufacturer(name="Daimler AG") daimlergroup.save() benz = Brand(name="Mercedes Benz") benz.save() benz.manufacturer = daimlergroup benzT = Car(name="T Modell") benzGLK = Car(name="GLK") benzSLK = Car(name="SLK") benzT.save() benzGLK.save() benzSLK.save() benzT.brand = benz benzGLK.brand = benz benzSLK.brand = benz harry = Driver(first_name="Harry", last_name="Klein") ayrton = Driver(first_name="Ayrton", last_name="Senna") harry.save() ayrton.save() harry.cars = [benzT, bmw3er, bmwX3] ayrton.cars.add([benzGLK, benzSLK, bmw7er, bmwX3]) benzTkey = Key(serial=12) benzTkey.save() bmwX3key = Key(serial=10039) bmwX3key.save() benzTkey.owner = harry bmwX3key.owner = ayrton
def runTest(self): vwgruppe = Manufacturer(name="VWGruppe") vwgruppe.save() vw = Brand(name="VW") vw.save() vw.manufacturer = vwgruppe golf = Car(name="Golf") golf.save() golf.brand = vw bulli = Car(name="Bulli") bulli.save() bulli.brand = vw kaefer = Car(name=u"Käfer") kaefer.save() kaefer.brand = vw # db setup complete # When setting a different foreign key, the formerly set foreign key # will be removed from the ReverseForeignKeyManager as well. # -> Make a new potential foreign key instance audi = Brand(name="Audi") audi.save() # Set the new foreign key on the ForeignKeyManager kaefer.brand = audi self.assert_(kaefer.brand is audi) self.assert_(kaefer not in vw.car_set) # golf and bulli are still part of vw.car_set self.assert_(golf in vw.car_set) self.assert_(bulli in vw.car_set) # Setting cars on a ReverseForeignKeyManager first removes all # entries from the relation, then adds the desired cars to brand. vw.car_set = [kaefer] # vw is now kaefer's brand again ... self.assert_(kaefer.brand is vw) self.assert_(kaefer in vw.car_set) # The other cars have been removed from the relation ... self.assert_(golf.brand is None) self.assert_(bulli.brand is None) # ... and Brand 'audi' is not related to car kaefer anymore, because a # Car can only have one Brand. self.assert_(len(audi.car_set) == 0) # You can use the ReverseForeignKeyManager.set method explicitly. audi.car_set.set([bulli, golf]) self.assert_(bulli in audi.car_set) self.assert_(golf in audi.car_set) self.assert_(bulli.brand is audi) self.assert_(golf.brand is audi)
def runTest(self): test_underscore = Car(name="Test_underscore") test_underscore.save() self.assert_(Car.objects.filter(name="Test_underscore")[0] is test_underscore)